Hello i want to pass my news_id to the twitter modal. But im getting a wrong data Can someone give me ideas on how to pass a data to the modal?
i delete the news with news_id "29"
and the deleted data is news with news_id "28"
here is my code.
<div class="container">
<table class="table table-bordered" >
<thead>
<tr>
<th width="60">ID</th>
<th width="200">News Title</th>
<th width="150">Date Posted</th>
<th>Content</th>
<th width="200">Image</th>
<th width="200">Action</th>
</tr>
</thead>
<tbody>
<?php
$stmt = mysqli_prepare($con, "SELECT * FROM news ORDER BY news_id");
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row['news_id']; ?></td>
<td><?php echo $row['news_title']; ?></td>
<td><?php echo $row['news_date']; ?></td>
<td><?php echo $row['news_content']; ?></td>
<td><img style="height:150px; width:200px;" src="<?php echo $row['news_image']; ?>" ></td>
<td>
<a class='btn btn-info left-margin' href="edit2.php?newsid=<?php echo $row['news_id'];?>" ><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a class='btn btn-danger delete-object' data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-remove"></span> Delete</a>
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
Are you sure you want to delete this?
</div>
<div class="modal-footer">
<a class='btn btn-danger left-margin' href="delete.php?newsid=<?php echo $row['news_id'];?>" >Yes</a>
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
here is my delete.php
<?php
include_once('connection.php');
$newsid = $_GET['newsid'];
if(isset($newsid)){
$stmt = mysqli_prepare($con, "DELETE FROM news WHERE news_id = ?");
mysqli_stmt_bind_param($stmt, "s", $newsid);
mysqli_stmt_execute($stmt);
header('location: edit.php');
}
?>
The error resides in the way you declare your modals.
Your first delete button for news_id 28 opens the modal with the id #myModal, but so does the second button for news_id 29, which opens the first modal with id #myModal, being the modal for news_id 28.
Proposed solution: Add the ID to the modal.
<a class='btn btn-danger delete-object' data-toggle="modal" data-target="#modal<?php echo $row['news_id'];?>"><span class="glyphicon glyphicon-remove"></span> Delete</a>
and
<div class="modal fade" id="modal<?php echo $row['news_id'];?>" role="dialog" aria-labelledby="myModalLabel">
should fix it.
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
i am trying to display and update read/unread messages. I have a column in database msg_read, that is by default has a value of 0. 0 means read, and 1 means unread. Need guidance to update the status when the message is read. With the guidance of Mr. Andre, the unread messages are showing Bold now, but when read, messages become normal (not bold).
<!-- Display User Messages Details Starts Here -->
<div class="tab-pane userprof-tab" id="tab-10">
<div class="table-responsive border-top">
<table class="table table-bordered table-hover mb-0 text-nowrap">
<thead>
<tr>
<th>Name</th>
<th>Jetski</th>
<th>Sender Email</th>
<th>Phone</th>
<th>Post Code</th>
<th>Message Date</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php
$SelectBoat = mysqli_query($con, "SELECT * FROM seller_contact WHERE seller_id='$user_id'");
while($row=mysqli_fetch_assoc($SelectBoat)){
$full_name = $row['full_name'];
$boatname = $row['boatname'];
$sender_email = $row['sender_email'];
$phone = $row['phone'];
$post_code = $row['post_code'];
$message = $row['message'];
$msg_date = $row['msg_date'];
$seller_id = $row['seller_id'];
$msg_read = $row['msg_read'];
?>
<?php
if(msg_read=="0"){
echo '<tr style="font-weight:900">
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row['boat_name'];?></td>
<td><?php echo $row['sender_email'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['post_code'];?></td>
<td><?php echo $row['msg_date'];?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>'
} ?>else {
echo '<tr>
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row['boat_name'];?></td>
<td><?php echo $row['sender_email'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['post_code'];?></td>
<td><?php echo $row['msg_date'];?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>'
} ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
I found some errors in your code and some structural failures.
if you are working with PHP in a HTML page, use alternative syntax
see:
https://www.php.net/manual/en/control-structures.alternative-syntax.php
you don't need to rewrite the complete row again for the read message, only put a condicional on the style attribute.
you was calling read_msg instead of $read_msg
new code:
<div class="tab-pane userprof-tab" id="tab-10">
<div class="table-responsive border-top">
<table class="table table-bordered table-hover mb-0 text-nowrap">
<thead>
<tr>
<th>Name</th>
<th>Jetski</th>
<th>Sender Email</th>
<th>Phone</th>
<th>Post Code</th>
<th>Message Date</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($SelectBoat)) : ?>
<?php
$full_name = $row['full_name'];
$boatname = $row['boatname'];
$sender_email = $row['sender_email'];
$phone = $row['phone'];
$post_code = $row['post_code'];
$message = $row['message'];
$msg_date = $row['msg_date'];
$seller_id = $row['seller_id'];
$msg_read = $row['msg_read'];
?>
<tr <?= ($msg_read == "0") ? 'style="font-weight:900"' : '' ?>>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['boat_name']; ?></td>
<td><?php echo $row['sender_email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['post_code']; ?></td>
<td><?php echo $row['msg_date']; ?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email']; ?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message']; ?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
UPDATE:
to update the status of the message just like i told you in the commnent. add this to your code:
add this sttribute to your view button:
onclick="readMSG(this, '<?php echo $row['id']; ?>')"
add this script and modify to work in your case:
<script>
function readMSG(attribute, id) {
$(attribute).parent().parent().css('font-weight', 'normal')
$.ajax({
type: "GET",
url: "readmsg.php",
data: {
id: id
},
success: function (){
$(attribute).parent().parent().css('font-weight', 'normal')
}
});
}
</script>
the php controller file readmsg.php:
$update = mysqli_query($con, "UPDATE `seller_contact` SET `read_msg`=1 WHERE `id` =" . $_GET['id']);
if ($update) {
return ['success' => 'success'];
} else {
return ['error' => 'error'];
}
try to understand the code and adapt to work in your case. if work, don't forgot to accept the answer ;)
<table class="table table-bordered">
<tr>
<th>Model Code</th>
<th>Size</th>
<th>Type</th>
<th>Price</th>
<th>Specs</th>
</tr>
<?php
while($row = $result->fetch_assoc()){
$f1=$row['model_code'];
$f2=$row['size'];
$f3=$row['type'];
$f4=$row['price'];
$f5=$row['specs'];
?>
<tr>
<td><?php echo $f1 ?></td>
<td><?php echo $f2 ?></td>
<td><?php echo $f3 ?></td>
<td><?php echo $f4 ?></td>
<td>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">SPECS</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title text-center" id="myModalLabel"><?php echo $f1 ?></h4>
</div>
<div class="modal-body">
<div class="froala-view">
<?php echo $f5 ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
} //while ends here
?>
</table>
I am trying to fetch data from my database.other section is ok. But which is inside the modal of bootstrap is not able to fetch new data. It only fetching the first data of the table.
What should i do?
Can someone tell me what I'm doing wrong here? I create a table which has a delete button in each row, when clicked pops up an modal and asks if I want to delete the row. But when i click on the button nothing happens. Why is this happen?(I suspect it may be because of the INNER JOIN) Any ideas how to solve this?
list_book.php (INNER JOIN):
<table class="table table-bordered table-hover" id="datatable">
<thead>
<tr>
<th>Número</th>
<th>Estante</th>
<th>Obra</th>
<th>Autor</th>
<th>Categoria</th>
<th>Ano Escolaridade</th>
<th>Observação</th>
<th class="text-center">Opções</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT B.number, B.shelf, B.title, B.author, B.obs, C.category_name, S.scholarity_name FROM book AS B
INNER JOIN category AS C ON C.id_category=B.category_id INNER JOIN scholarity AS S ON S.id_scholarity=B.scholarity_id";
$res = mysql_query($query);
mysql_set_charset('utf-8');
if (!$res) {
echo "Erro ao executar a query";
} else {
while ($dados = mysql_fetch_array($res)) {
?>
<tr>
<td><?php echo $dados['number']; ?></td>
<td><?php echo $dados['shelf']; ?></td>
<td><?php echo $dados['title']; ?></td>
<td><?php echo $dados['author']; ?></td>
<td><?php echo $dados['category_name']; ?></td>
<td><?php echo $dados['scholarity_name']; ?></td>
<td><?php echo $dados['obs']; ?></td>
<td class="text-center">
<span class="fa fa-edit"></span> Editar
<a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#<?php echo $dados['id']; ?>" data-whatever="#mdo"><span class="fa fa-trash"></span> Apagar</a>
<?php include('modal_delete.php'); ?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
MODAL(BOOTSTRAP):
<div class="modal" id="<?php echo $dados['id_book']; ?>" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-left">Apagar</h4>
</div>
<div class="modal-body text-left">
<p>Deseja apagar este registro?</p>
</div>
<div class="modal-footer">
Apagar
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
</div>
</div>
</div>
delete_book.php
<?php
session_start();
require("../../conexao/conexao.php");
$id = $_GET["id"];
$query= "DELETE FROM book WHERE id_book = $id";
$delete= mysql_query($query);
if(!$delete){
echo "Erro!! Não foi possivel apagar dado.";
}
else{
echo"Dado removido com sucesso!";
header("Location: ../list/list_book.php");
}?>
I'm new on this so if something's wrong... please help me.
You must not do your delete request with GET , Create a form instead of anchor , and place a button inside it. make your request with post method
When you should use get & post request
Change this line :
$query= "DELETE FROM book WHERE id_book = $id";
to
$query= "DELETE FROM book WHERE id_book = ".$id;
I want to show more info about the file from the database using modal instead adding more columns to the table, and also instead of opening new page.
here is my code
<table id="file" class="table table-bordered table-striped table-hover">
<?php
$result = mysql_query("SELECT * FROM files")
?>
<thead>
<tr>
<th>ID <i class="fa fa-sort"></i></th>
<th>Name <i class="fa fa-sort"></i></th>
<th>Size <i class="fa fa-sort"></i></th>
<th>Delete </th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_array( $result )) {
?>
<tr class="record">
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['size']; ?> </td>
<td><div align="center"></div></td>
<td><div align="center"></div></td>
</tr>
<?php
}
?>
</tbody>
</table>
Thanks in advance.
If there any other way place say it.
You can replace the following data-target and modal ID with PHP, if that is what you mean.
<!--This calls the modal by data-target ID -->
<a data-toggle="modal" data-target="#myModal">This link calls the modal</a>
<!--modal content-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel"></h4>
</div>
<div class="modal-body">
<!-- your content goes here -->
</div>
<div class="modal-footer">
<center><button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button></center>
</div>
</div>
I am new to programming and trying to implement bootstrap modal to display row data from a mysql table into the modal window.
I have tried the solution found on stackoverflow "Pull information from mysql table to bootstrap modal to edit" by link. But could not able to display the particular row with the $row['SFID'].
I can pull table data but when I click the edit button in front of any row it always show the last row id
and doesn't display the data in the input box on the modal to edit the data???.
Here I am till now, Please help me out.
<table class="table table-bordered" width="100%">
<thead>
<tr>
<th>SFID</th>
<th>Company</th>
<th>Product</th>
<th>Product Line</th>
<th>Dealer Class</th>
<th>Status</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM tblcustomer";
$stmt = $db->prepare($query);
$stmt->execute();
foreach ($stmt as $row): ?>
<tr>
<?php $rowID = $row['SFID']; ?>
<td><?php echo $row['SFID']; ?></td>
<td><?php echo $row['CompanyName']; ?></td>
<td><?php echo $row['Product']; ?></td>
<td><?php echo $row['ProductLine']; ?></td>
<td><?php echo $row['DealerClass']; ?></td>
<td><?php echo $row['RequestStatus']; ?></td>
<td style="text-align: center">
<div class="btn-toolbar">
<div class="btn-group">
<a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
<?php echo "<a class='btn update' href='#editModal' data-sfid='".$row['SFID']."' role='button' data-toggle='modal'>Edit</a>"; ?>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
</table>
<div id="editModal" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Edit Customer Details</h3>
</div>
<div>
<form class="contact">
<fieldset>
<div class="modal-body">
<?php echo $row['SFID']; ?>
<ul class="nav nav-list">
<li class="nav-header">SFID</li>
<li><input class="input-xlarge" type="text" name="mysfid" id="mysfid"></li>
<!--<li class="nav-header">Company</li>
<li><input class="input-xlarge" value=" " type="text" name="mycompany"></li>
<li class="nav-header">Dealer Class</li>
<li><input class="input-xlarge" value=" " type="text" name="mydealerclass"></li> -->
</ul>
</div>
</fieldset>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-success" id="submit">Approved</button>
Close
</div>
</div>
<script>
$(document).ready(function(){
$('a.edit').click(function(){
var sfid = $(this).data('sfid');
var company = $(this).data('company');
var dealerclass = $(this).data('dealerclass');
$('#mysfid').val(sfid);
$('#mycompany').val(company);
$('#mydealerclass').val(dealerclass);
});
});
</script>
Thanks for your help.
<table class="table table-bordered" width="100%">
<thead>
<tr>
<th>SFID</th>
<th>Company</th>
<th>Product</th>
<th>Product Line</th>
<th>Dealer Class</th>
<th>Status</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM tblcustomer";
$result = mysql_query($query);
$i=1;
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<?php $rowID = $row['SFID']; ?>
<td><?php echo $row['SFID']; ?></td>
<td><?php echo $row['CompanyName']; ?></td>
<td><?php echo $row['Product']; ?></td>
<td><?php echo $row['ProductLine']; ?></td>
<td><?php echo $row['DealerClass']; ?></td>
<td><?php echo $row['RequestStatus']; ?></td>
<td style="text-align: center">
<div class="btn-toolbar">
<div class="btn-group">
<a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
<a class="btn update" href="#editModal<?php echo$i?>" data-sfid='"<?php echo $row['SFID'];?>"' data-toggle="modal">Edit</a>
<!--Yor Edit Modal Goes Here-->
<div id="editModal<?php echo $i; ?>" class="modal hide fade in" role="dialog" ria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Edit Customer Details</h3>
</div>
<div>
<form class="contact">
<fieldset>
<div class="modal-body">
<?php echo $row['SFID']; ?>
<ul class="nav nav-list">
<li class="nav-header">SFID</li>
<li><input class="input-xlarge" type="text" name="mysfid" id="mysfid"></li>
<!--<li class="nav-header">Company</li>
<li><input class="input-xlarge" value=" " type="text" name="mycompany"></li>
<li class="nav-header">Dealer Class</li>
<li><input class="input-xlarge" value=" " type="text" name="mydealerclass"></li> -->
</ul>
</div>
</fieldset>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-success" id="submit">Approved</button>
Close
</div>
</div>
</div>
</div>
</td>
</tr>
<?php $i++; } ?>
</table>
take $i = 1 above for loop and increament it in each iteration of for loop..so it will take each records
Your Form
<a class='btn update' href='#editModal<?php echo $i;?>' data-sfid='".$row['SFID']."' role='button' data-toggle='modal'>Edit</a>
Modal Window
<div id="editModal<?php echo $i;?>" class="modal hide fade in" style="display: none; ">
That's because you fill the edit modal with the $row data, which by then, is on the last item.
To get data for a specific row, you could create a javascript object/array and then fetch the data by making use of the data-rfid parameter in the "Edit" link. Or you can fetch the row with Ajax for example.