Pass a current $row value to Bootstrap Modal - php

Table:
<table class="table table-bordered">
<thead>
<tr>
<th>Works At</th>
<th>First Name</th>
<th>Middle Initial</th>
<th>Last Name</th>
<th>Start Date</th>
<th>SSN#</th>
<th>Modify</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM employees";
$stmt = $db->prepare($query);
$stmt->execute();
foreach ($stmt as $row): ?>
<tr>
<td><?php echo $row['rest_id_employees']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['minit']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['start_date']; ?></td>
<td><?php echo $row['ssn']; ?></td>
<td style="text-align: center">
<div class="btn-toolbar">
<div class="btn-group">
<a class="btn btn-danger" href="<?php echo $_SERVER['PHP_SELF'].'?ssn='.urlencode($row['ssn']); ?>#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
<a class="btn btn-warning" href="#editModal"><i class="icon-wrench icon-white"></i> Edit</a>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
</table>
My goal is to pass the value of $row['ssn'] into my #delModal. I assume the problem lies within:
<a class="btn btn-danger" href="<?php echo $_SERVER['PHP_SELF'].'?ssn='.urlencode($row['ssn']); ?>#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
It is my understanding that with the above syntax I should be able to use $_GET['ssn'] inside the Bootstrap Modal to pull the value, but when I var_dump($ssn) the value only NULL is printed.
Here is the Modal:
<div class="modal hide" id="delModal" aria-hidden="true">
<div class="modal-header">
<h3 style="color: red">WARNING</h3>
</div>
<div class="modal-body">
<?php
$ssn = $_GET['ssn'];
var_dump($ssn);
?>
<h4>You are about to delete --- from your Employee database.</h4>
<p>An employee cannot be recoverd once they have been deleted. They can be added to the system again, but
all data about the employee will be lost if you delete them. Are you sure you mean to delete this
employee from the database?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a class="btn btn-danger" href="deleteemployee.php?ssn=<?php echo urlencode($ssn); ?>"><i class="icon-trash icon-white"></i> Delete</a>
</div>
</div>
The overall purpose of this implementation is to use the Modal as a means to not accidentally delete an employee by prompting the user with a warning. I cannot just use $row within the Modal as it will pull the last row every time, and I'm sure there's a better way of achieving what I'm trying to do than what I'm trying to do here, but I'm not sure what that way is.

Hello I had the same issue and almost the same code as you are using. But I able to solve that issue later by passing my parameter value as href id.
I had a button named READ, to open my model window. Here is the HTML code for my button.
<a button type="button" href="#<?php echo''.$u_id.'';?>" data-toggle="modal" class="btn btn-primary"> READ </a>
Here in that above code, you can see i have used id(column name and the primary key) of the message as the href id!
And here is the my code for MODEL WINDOW
<div class="modal" id="<?php echo''.$u_id.'';?>">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Create public Event</h4>
</div>
<div class="modal-body">
<?php echo '.$u_id.';?>
</div>
</div>
</div>
so once I passed the id (parameter) in to the modal, I can get other values using simple sql query.
So for your code also, i'm suggesting you to pass your primary key column value (Eg:id) as the button href id and div id.

the best way is to use Jquery Ajax call
like the following step: -
1. Create a function which get a parameter (or more).
2. Which is use to process Ajax post method.
3. And on return print HTML data in the modal div.
4. Call that function along with modal button "onClick" event...
And rest will be done easily the best and simple way to do that
http://infotonicsmedia.com

I was faced with a simular situation.
My solution was:
private function pri_show_weekcontent($p_sCourse){
for($iWeekcounter=1;$iWeekcounter<=10;$iWeekcounter++){
// Fetch the nesseray data from the database
$aCellContent = $this->fetchAssignmentscontents($p_sCourse,$iWeekcounter);
// Create an unique ID for the modal, in my case course+weeknumber
echo("<div id='".$p_sCourse.$iWeekcounter."' class='modal fade' role='dialog'>");
// Build the modal
echo("<div class='modal-dialog'>");
echo("<div class='modal-content'>");
echo("<div class='modal-header'>");
echo("<button type='button' class='close' data-dismiss='modal'>×</button>");
echo("<h4 class='modal-title'>Toelichting opdracht</h4>");
echo("</div>");
echo("<div class='modal-body'>");
// show the content from the database
echo("<p>".$aCellContent[0][6]."</p>");
echo("</div>");
echo("<div class='modal-footer'>");
echo("<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>");
echo("</div>");
echo("</div>");
echo("</div>");
echo("</div>");
if(!empty($aCellContent)){
if($_SESSION['verifiedUser']=='true'){echo("<td align='center' width='8%' bgcolor='white'><a href='editassignment.php?rn=".$aCellContent[0][0]."'><span title='".$aCellContent[0][6]."'>".$aCellContent[0][5]."</span></a></td>"); }
// call the unique ID model with a click.
else{echo("<td align='center' width='8%' bgcolor='white'><a data-toggle='modal' data-target='#".$p_sCourse.$iWeekcounter."'>".$aCellContent[0][5]."</a></td>");}
}else{
if($_SESSION['verifiedUser']=='true'){echo("<td align='center' width='8%' bgcolor='lightgreen'><a href='addassignment2.php?cn=".$_SESSION['sClassname']."&oo=".$p_sCourse."&pn=".$_SESSION['iPeriodNumber']."&wn=".$iWeekcounter."'>Geen huiswerk</a></td>");}
else{echo("<td align='center' width='8%'> </td>");}
}
}
}

Rather than passing value as get directly echo your values in model, and get an advantage of both being in same file.
call your model like this
<a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-trash icon-white"></i> Delete</a>
In model try like this
<div class="modal hide" id="delModal" aria-hidden="true">
<div class="modal-header">
<h3 style="color: red">WARNING</h3>
</div>
<div class="modal-body">
<?php echo $row['ssn']; ?>
<h4>You are about to delete --- from your Employee database.</h4>
<p>An employee cannot be recovered once they have been deleted. They can be added to the system again, but
all data about the employee will be lost if you delete them. Are you sure you mean to delete this
employee from the database?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a class="btn btn-danger" href="deleteemployee.php?ssn=<?php echo urlencode($ssn); ?>"><i class="icon-trash icon-white"></i> Delete</a>
</div>
</div>
Hope this helps.

Related

Bootstrap modal doesn't get user ID

I have a problem with a bootstrap modal. I'm using PHP.
In an admin panel there is a table with the list of the users and the possibility to edit or delete a user's profile. For the delete I want to create a modal for the confirm of the delete but, when I click on "confirm" button inside the modal, the modal gets by default the user ID of the first user in the table and not the user ID of the selected user.
Here is the code:
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php var_dump($utente['idUser']); ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
If I make a var_dump of $utente['idUser'] before the modal, it gets the right user ID. If I make it inside the modal it gets by default the first ID, as I said.
Notice that every modal trigger button has a data-target attribute to define which modal will be opened.
In your case, the button of every row you used to triggered the modal have the same data-target, which is #confirmDelete. These modals behind also has the same id called #confirmDelete, so every time you hit the modal trigger button (all had the same data-target) then eventually it will shows up the very first modal element.
For a better understanding, compare my code to yours and see the differences.
<?php foreach ($utenti as $utente) { ?>
<tr>
<th scope="row"> <?php echo $utente['idUser']?> </th>
<td><?php echo $utente['nome']." ".$utente['cognome']?></td>
<?php if($_SESSION['role'] == 1) {?>
<td><?php echo $utente['az']?></td>
<?php } ?>
<td><?php echo $utente['email']?></td>
<td class="text-warning"><a
href="<?php echo 'editUser.php?user='.$utente['idUser']?>"><i
class="fas fa-edit text-warning"></i></a></td>
<!-- <td class="text-warning"><i class="fas fa-trash-alt text-danger"></i></td> -->
<td class="text-danger">
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>"><i
class="fas fa-trash-alt text-danger"></i><?php var_dump($utente['idUser']); ?>
</button>
</td>
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
Attenzione <?php echo $utente['idUser']; ?></h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Continuando eliminerai l'utente in maniera irreversibile</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger "
><a
class="text-white btn-modal-confirm"
href="<?php echo '?action=delete&user='.$utente['idUser']?>"
>Elimina</a>
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Indietro
</button>
</div>
</div>
</div>
</div>
</tr>
<?php }?>
In the above code, I gave every pair of modal elements (modal trigger button and modal ID) a unique data-target value and a unique element id.
...
<button type="button" class="btn" data-toggle="modal"
data-target="#confirmDelete_<?php echo $utente['idUser']; ?>">
...
<div class="modal" tabindex="-1" role="dialog" id="confirmDelete_<?php echo $utente['idUser']; ?>">
...
Now each pair of modal elements have their own ids and they should be working the way you wanted.

Jquery and Bootstrap 4 dataTable delete row on page 2 not working

Good day! I have a jquery and bootstrap 4 datatable with pagination and a delete button at each row but after the page 1 is full of data it will go to page 2. The problem is the data in page 2 with a delete button row is not working but in page 1 the delete button works perfectly..
Here's my table code:
<table class="table table-hover table-bordered" id="questionTable">
<thead>
<tr>
<th>No.</th>
<th>Student</th>
<th>Subject Code</th>
<th>Semester</th>
<th>SchoolYear</th>
<th>Professor</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $result -> fetch_object()): ?>
<tr>
<td><?php echo $row->Subject_Student_Taken;?></td>
<td><?php echo $row-> Student_ID;?></td>
<td><?php echo $row-> Subject_Code;?></td>
<td><?php echo $row-> Term_Name;?></td>
<td><?php echo $row-> SY_Name;?></td>
<td><?php echo $row-> Faculty_ID;?></td>
<td class="text-center">
<a class="btn btn-sm btn-outline-primary text-muted" href="SubjectTaken_edit.php?Subject_Student_Taken=<?php echo $row->Subject_Student_Taken;?>">Edit</a>
<a data-fid="<?php echo $row->Subject_Student_Taken;?>" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger delete-faculty-btn text-muted" >Delete</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Here's my delete code:
if(!empty($_POST['delete'])) {
$id = $_POST['delete'];
$data->delete_StudentTakenHandle($id, $conn);
}
Function code:
function delete_StudentTakenHandle($id, $conn){
$sql = "DELETE FROM subject_taken_handle WHERE Subject_Student_Taken=? ";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
}
Modal code:
<div class="modal fade" id="delete_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Confirmaiton</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="alert alert-danger" role="alert">
Are you sure you want to delete selected record?
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<input type="hidden" name="delete" id="row-id-to-delete">
<button type="submit" class="btn btn-danger" >Yes</button>
</div>
</div>
</div>
</div>
And here's my jquery code:
<script>
$('.delete-faculty-btn').on('click',function(){
$("#row-id-to-delete").val($(this).data('fid'));
});
</script>
The error is, most likely, caused by this:
only the elements from the first page are displayed (existing) when you set your jQuery event.
Since the jQuery.on function attaches events only to existing elements, the event is not attached to elements from other pages.
Solution:
You should use the table's click event as a wrapper for attaching events to delete buttons.
This way, each time you click and change the page, delete buttons will exist, and the jQuery.on function will be able to attach event for delete to them.
You should wrap your event attach code like this:
$("#questionTable").on("click", ".delete-faculty-button", function(){
// attach your delete event here
});
Read more in the documentation of jQuery.on.
Use the delegated format of on instead of click..on() | jQuery API Documentation
$('.delete-faculty-btn').on('click',function(){
$("#row-id-to-delete").val($(this).data('fid'));
});
becomes:
$(document).on("click", ".delete-faculty-btn", function () {
$("#row-id-to-delete").val($(this).data('fid'));
});

How to pass data from table of records to Modal for editing purposes

I know this has been asked a lot but I haven't found any of the answers to the issue to be very fruitful. I essentially have a table inside a view that is displayed using a foreach loop in PHP, this spits out the records one by one and appends some 'Action' buttons in the last column (download, delete and edit). The download and delete functions work perfectly, I just pass the ID in from each record in the foreach loop, job done.
I'm having a lot of issues with my modal though, it only ever displays the data from the first record when I echo the data in the 'value' field of each input. I'm really stumped on how to make this functionality work (JQuery is not my strongest language). I've seen some responses talking about Ajax, but I'd rather not use Ajax in this project. I'm using the codeigniter framework also for some more info.
I think the issue is that the modal is only created once when the foreach loop starts running, hence why it only ever has the first record data inside the modal, any help to get around this so I can edit each individual record inside the table would be great! Thanks for the help.
HTML/PHP:
<div class="container" id="widecontainer">
<h1 id="title">VMS Failure Records</h1>
<br>
<!-- print table with results onto view.php -->
<table class="table table-bordered" id="record">
<?php if($results) : ?>
<thead>
<tr style="background-color: #d3d3d3;">
<th>ID</th>
<th>VSM S/N</th>
<th>VM S/N</th>
<th>Project</th>
<th>Site</th>
<th>Install Loc</th>
<th>Observed During</th>
<th>Comments</th>
<th>Reported By</th>
<th>Replaced With</th>
<th>Date</th>
<th>Failure Classification</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!-- foreach result place it in row in table -->
<?php foreach($results as $res) : ?>
<tr>
<td><?php echo $res['id'] ?></td>
<td><?php echo $res['vsm_sn'] ?></td>
<td><?php echo $res['vm_sn'] ?></td>
<td><?php echo $res['project'] ?></td>
<td><?php echo $res['site'] ?></td>
<td><?php echo $res['install_location'] ?></td>
<td><?php echo $res['observed_during'] ?></td>
<td><?php echo $res['comments'] ?></td>
<td><?php echo $res['reported_by'] ?></td>
<td><?php echo $res['replaced_with'] ?></td>
<td><?php echo $res['date'] ?></td>
<td><?php echo $res['classification'] ?></td>
<td>
<?php echo form_open('/pages/delete/'. $res['id']); ?>
<button type="submit" class="btn btn-danger delete_btn" id="delete_btn" target="#confirmation">
<i class="fa fa-trash" aria-hidden="true"></i>
</button>
<!-- Modal displays when user clicks delete, asking them to confirm the deletion -->
<div id="confirm" name="confirm" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Record <i class="fa fa-trash" aria-hidden="true"></i></h4>
</div>
<div class="modal-body">
<p style="color: red;"><strong>Deleting this record will delete .PDF and QRCode Data.</strong></p>
<p>Are you sure you want to delete this record?</p>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-danger" id="delete">Delete</button>
<button type="button" data-dismiss="modal" class="btn">Cancel</button>
</div>
</div>
</div>
</div>
</form>
<!--Modal displays to allow user to download the selected record. -->
<?php echo form_open('/pages/downloadFile/'. $res['id']); ?>
<button type="submit" class="btn btn-primary" alt="Download .pdf">
<i class="fa fa-download" aria-hidden="true"></i>
</button>
</form>
<form>
<button type="button" class="btn btn-success update_btn" id="update_btn" data-toggle="modal" data-target="#myModal"
vsm-sn="<?php echo $res['vsm_sn'];?>" record-id="<?php echo $res['id']; ?>">
<i class="fa fa-pencil" aria-hidden="true"></i>
</button>
</form>
<!-- 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">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Update Record</h4>
</div>
<div class="modal-body">
<form id="profileForm">
<input type="hidden" class="form-control" name="id" value="">
VSM SN : <input class="form-control" name="vsm_sn" value="" placeholder="VSM SN">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php endforeach; ?>
<!-- If there are no results, display table headings and message -->
<?php elseif(!$results) : ?>
<thead>
<tr>
<th>ID</th>
<th>VSM S/N</th>
<th>VM S/N</th>
<th>Project</th>
<th>Site</th>
<th>Install Loc</th>
<th>Observed During</th>
<th>Comments</th>
<th>Reported By</th>
<th>Replaced With</th>
<th>Date</th>
<th>Failure Classification</th>
</tr>
</thead>
<tbody>
<h3 style="color: red;">No Results to Display</h3>
</tbody>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
JQUERY:
$('#myModal').on('show.bs.modal', function (e) {
// get information to update quickly to modal view as loading begins
var opener=e.relatedTarget;//this holds the element who called the modal
//we get details from attributes
var vsm_sn=$(opener).attr('vsm-sn');
//set what we got to our form
$('#profileForm').find('[name="vsm_sn"]').val(vsm_sn);
});
The best way to go about it , is to use events hooks which is tied to the modal to help manage dynamism on modals.
Take for example, you want to pass information from the for loop to the modal you can do this using just a modal and then update the modal as it opens.
check bootstrap docs you will see this hook for modal
show.bs.modal
we then use attributes from the button to pick out our information we are going to display on the modal, e.g
<button user-id="<?php echo $data[$i]->userID; ?>" first-name="<?php echo $data[$i]->firstname;?>">Edit</button>
then we can use Jquery to pick it up and hook it when the modal is opening
See for example code here, replace the static repeat with your dynamic coding
$('#myModal').on('show.bs.modal', function (e) {
// get information to update quickly to modal view as loading begins
var opener=e.relatedTarget;//this holds the element who called the modal
//we get details from attributes
var firstname=$(opener).attr('first-name');
//set what we got to our form
$('#profileForm').find('[name="firstname"]').val(firstname);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<table class="table">
<thead>
<tr>
<th>SN</th>
<th>Firstname</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Theophilus</td>
<td>
<button class="btn btn-primary" first-name="Theophilus" data-toggle="modal" data-target="#myModal">
Edit
</button>
</td>
</tr>
<tr>
<td>3</td>
<td>Omoregbee</td>
<td>
<button class="btn btn-primary" first-name="Omoregbee" data-toggle="modal" data-target="#myModal">
Edit
</button>
</td>
</tr>
<tr>
<td>3</td>
<td>Endurance</td>
<td>
<button class="btn btn-primary" first-name="Endurance" data-toggle="modal" data-target="#myModal">
Edit
</button>
</td>
</tr>
</tbody>
</table>
<!-- 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">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<form id="profileForm">
Firstname : <input class="form-control" name="firstname" value="" placeholder="firstname">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Run it and see the result, so you can change the table style to use loop in php and then echo your data as attribute.
Hope it helps.
assign an id to modal body, e.g. : modal-body
declare a variable to store the HTML for the modal body, e.g. var modalHTML = ''
then append the contents in the for loop to that variable.
after that append the variable to the modal body, using $('#modal-body).append($(modalHTML)).

How to pass GET value to bootstrap modal

i have a bootstrap modal and am trying to pass a unique value through GET to the modal but the modal keeps getting the same unique value instead of the value of the table row i clicked. Been struggling with this for days and cant seem to figure out why it's not working. The code is below
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
<td>
<div class="btn-group">
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_check_alt2"></i></a>
<a class="btn btn-primary" href="edit_property.php?house=<?php echo $row['nickname']; ?>"><i class="icon_plus_alt2"></i></a>
<a class="btn btn-danger" data-toggle="modal" data-target="#myDelete"><i class="icon_close_alt2"></i></a>
</div>
<div id="myDelete" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Property</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to pull out this property from your list of Properties, as all data regarding this property will be lost permanently <?php echo $row['nickname']; ?></p>
</div>
<div class="modal-footer">
<a class="btn btn-success" href="view_property.php?house=<?php echo $row['nickname']; ?>">Delete</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</td>
HTML ids should be unique, you're using the same id for every iteration.
Try using:
id="myDelete<?php echo $row['id'] ?>"
data-target="#myDelete<?php echo $row['id'] ?>"
Hope this helps!

bootstrap modal problems delete data

i'm trying to delete a row from my table using a bootstrap modal delete confirmation.
But the problem is, when i try to delete the one row mon my table, the one that gets deleted is the first row(always). Why is this happen? Any ideas?
HTML(table)
<table class="table table-bordered table-hover clearfix" id="example">
<thead>
<tr>
<th>ID de Funcionário</th>
<th>Sobrenome</th>
<th>Nome</th>
<th>Cargo</th>
<th>Telefone</th>
<th class="text-center">Opção</th>
</tr>
</thead>
<tbody>
<?php
$query="SELECT * FROM employee AS E ORDER BY name";
$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['func_id']; ?></td>
<td><?php echo $dados['last_name']; ?></td>
<td><?php echo $dados['name']; ?></td>
<td><?php echo $dados['position']; ?></td>
<td><?php echo $dados['phone']; ?></td>
<td class="text-center">
<a class="btn btn-danger btn-xs" data-toggle="modal" data-target="#delete" data-whatever="#mdo">
<span class="fa fa-trash">Apagar</span>
</a>
<?php include('modal_delete.php');?>
</td>
</tr>
<?php
}}
?>
</tbody>
Modal_delete.php(include)
<div class="modal" id="delete" 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
<a type="button" class="btn btn-default" data-dismiss="modal">Cancelar</a>
</div>
</div>
</div>
Delete_reg.php
<?php
require("../../conection/conection.php");
$id =$_GET["id"];
$query= "DELETE FROM employee WHERE idemployee= $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_reg.php");
}?>
All the possible solution that i found on the website don't solve my problem... can someone please help me? Thank you!!!
Say you have 5 rows. That means for each row, you generate a modal. Each of these 5 modals has id="delete". When you click on the link to open the modal, the bootstrap code opens up the first modal that matches $('#delete'), which means it will delete the first row.
You have two solutions:
You add in the echo $dados['idemployee']; in the delete button's data-target, and in the modal's id. (Not optimal, because you're still generating a modal's worth of code per table row)
You make only 1 modal, and add a click() event to the delete button, which updates the Apagar button's href with the proper id.

Categories