I have wrote an HTML table presenting users' information from an SQL database.
<table class="table table-hover">
<tr>
<th>Username</th>
<th>Password</th>
<th>Role</th>>
<th>Attribute</th>
</th>
<th>Ideas/Score</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php while($result=m ysqli_fetch_array($fetch)) { $usertoattrquery="SELECT * FROM usertoattr WHERE usertoattr.user_id=" .$result[user_id]; $attrfetch=mysqli_query($con,$usertoattrquery) or die ( 'could no connect with instructions'); echo "<tr><td>".$result[ "username"]. "</td><td>" .$result[ "password"]. "</td>
<td>" .$result[ "role"]. "</td><td>only for Eval</td><td>coming soon</td><td>
<a href='#editUser' role='button' class='btn' data-toggle='modal'>Edit</a> </td>
<td><button class='btn'onclick='deleteConfirm()'>Delete</button> </td></tr>"; } ?>
</table>
I would like when someone pushes the button "Edit" in a specific row a Modal to be opened and the values from this table row to be copied (for example name and password) inside the modal.
<div id="editUser" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<h3 id="myModalLabel">Edit User</h3>
User Name
<input type="text">
<br>User Password
<input type="text">
<br>User Role
<select>
<option>Evaluator</option>
<option>Observer</option>
</select>
<br>User Attribute
<select multiple>
<option>Economics</option>
<option>Software</option>
<option>Management</option>
</select>
<br>
<button class='btn btn-primary'>OK</button>
<button class='btn' data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
How is that possible?
You could possibly add an id to each of your <td> (just make sure each one is different multiple id's are bad!!)
Then have your javascript use that value (.innerHTML) to set the value of your input text inside your modal.
Related
I'm currently working on a project that envolves me using a admin dashboard. As apart of this i need to be able to display all users in the database and then edit them.
I want to be able to open the modal to edit the users, but whenever i open the modal its displaying the first entries data. How can i change this to display the different data for each line and then edit said data.
This is my current Code
<?php
include_once '../dashboard/dashHeader.php';
?>
<section>
<table class="table table-striped" style="border: 3px solid;">
<thead>
<tr>
<th>UID</th>
<th>Full Name</th>
<th>Email</th>
<th>User Name</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
include_once('../../includes/dbh.inc.php');
$sql = "SELECT * FROM users ORDER by usersId";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){
if ($row['type'] == 1) {
$role = "Admin";
} else {
$role = "User";
}
echo "<tr>";
echo "<td>".$row['usersId']."</td>";
echo "<td>".$row['usersName']."</td>";
echo "<td>".$row['usersEmail']."</td>";
echo "<td>".$row['usersUid']."</td>";
echo "<td>".$role."</td>";
echo '<td>
<button type="button" class="btn btn-link btn-sm btn-rounded" data-mdb-toggle="modal" data-mdb-target="#exampleModal">
Edit
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Editing: '.$row['usersUid'].'</h5>
</div>
<div class="modal-body">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-mdb-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</td>';
echo "</tr>";
}
?>
</tbody>
</table>
</section>
<?php
include_once '../dashboard/dashFooter.php';
?>
Without seeing what your JavaScript is doing, it's hard to debug
But first...
... you really need to fix the security of this page. This is vulnerable to HTML injection. You MUST escape all dynamic content (eg from your database), otherwise at best you will show mangled data, at worst you make it easy for a hacker to inject any code they like into your web page. At a minimum you should be running htmlspecialchars().
Ex echo "<td>".htmlspecialchars($row['usersName'])."</td>";
So this is my code, its a Laravel .blade.php but anyway my problem is with this HTML / PHP code.
The problem is that when I press on any delete button I get the same id, I get cat->id = 1. when I press the 'delete' button on the laptop row, I should get in my delete confirmation the id number: 2, but I still get the number: 1 and its that way with any other row
<div class="">
<div class="box">
<div class="box-header">
<h3 class="box-title">All Categories</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Id</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
#foreach($categories as $cat)
<tr>
<td>{{$cat->nombre}}</td>
<td>{{$cat->id}}</td>
<td>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
For the modal im using the next code:
<div class="modal modal-danger fade" id="delete" 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 text-center" id="myModalLabel"><span>Delete Confirmation</span><</h4>
</div>
<form action="{{route('categories.destroy','test')}}" method="post">
{{method_field('delete')}}
{{csrf_field()}}
<div class="modal-body">
<p class="text-center">
<span>Are you sure you want to delete this {{ $cat->id}}?</span>
</p>
<input type="hidden" name="category_id" id="cat_id" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"><span>No, Cancel</span></button>
<button type="submit" class="btn btn-warning"><span>Yes, Delete</span></button>
</div>
</form>
</div>
</div>
I pressed the delete button on the Laptop row, and I get the id number:1 when I should get id number:2. this apply on every row
This is because everytime you click on the delete button it calls the same model with the same ID. Change you Button ID
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal" data-target="#delete-{{$cat->id}}">Delete</button>
And the modal ID as well
<div class="modal modal-danger fade" id="delete-{{$cat->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
NOTE: You might need to keep the modal box in foreach loop so that delete button is mapped to the correct modal box
I have a PHP file manage-users.php which contains bootstrap Data-table. Which shows data from MySQL users Table .
code of Data-table is :
<table id="myTable" class="table table-striped table-hover">
<thead>
<tr>
<th>
<span class="custom-checkbox">
<input type="checkbox" id="selectAll">
<label for="selectAll"></label>
</span>
</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($conn,"SELECT * FROM users") or die("Unable to qet all users data " . mysqli_error($conn));
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo '<td>
<span class="custom-checkbox">
<input type="checkbox" id="checkbox1" name="options[]" value="1">
<label for="checkbox1"></label>
</span>
</td>';
echo "<td>".$row['FIRST_NAME']."</td>";
echo "<td>".$row['LAST_NAME']."</td>";
echo "<td>".$row['EMAIL']."</td>";
echo "<td>".$row['PHONE']."</td>";
echo '
<td>
<i class="material-icons" data-toggle="tooltip" title="Edit"></i>
<i class="material-icons" data-toggle="tooltip" title="Delete"></i>
</td>
';
echo "</tr>";
}
?>
</tbody>
</table>
In the same page, I have two bootstrap modals to delete and edit data .
I'm working on Delete now.
Delete Modal Code is :
<div id="deleteEmployeeModal<?php echo $row['USER_ID']; ?>" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form>
<div class="modal-header">
<h4 class="modal-title">Delete User</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete these Records?</p>
<p class="text-warning"><small>This action cannot be undone.</small></p>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<button class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
Now the problem is that, that i can not delete data because in Delete Modal the $row['USER_ID] is empty. there is no problem in retrieving data from the server because the $row['USER_ID] is showing showing on every user specific delete button. what should i do now ?
If question needs more explanation, i'll explain.
Upon opening the modal, you basically set a hidden field so that when you submit the form, it will send delete_user_data.php?id=YOUR-VALUE.
1) Store the ID in the HTML. A good place might be the link that opens the modal. Also, remove data-toggle attribute because we will be opening the modal dynamically.
echo '<i class="material-icons" data-toggle="tooltip" title="Delete"></i>';
2) Only use one modal for deleting.
<div id="deleteEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<!-- give your form an action and method -->
<form action="delete_user_data.php" method="GET">
<div class="modal-header">
<h4 class="modal-title">Delete User</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete these Records?</p>
<p class="text-warning"><small>This action cannot be undone.</small></p>
</div>
<div class="modal-footer">
<!-- add a hidden input field to store ID for next step -->
<input type="hidden" name="id" value="" />
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<!-- change your delete link to a submit button -->
<button class="btn btn-danger" type="submit">Delete</button>
</div>
</form>
</div>
</div>
</div>
3) Open the modal dynamically.
$(function () {
$('a.delete').click(function (e) {
e.preventDefault();
var link = this;
var deleteModal = $("#deleteEmployeeModal");
// store the ID inside the modal's form
deleteModal.find('input[name=id]').val(link.dataset.id);
// open modal
deleteModal.modal();
});
});
Use similar approach for doing the Edit modal.
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)).
i want to pass mysql select value (UID) to css popup.this is my code what I have tried.but it's display empty textbox.this pop up button display when click on delete button.i want to pass relavent user UID to popup.
<table class="table table-striped table-bordered responsive">
<thead>
<tr>
<th>User ID</th>
<th>Facebook ID</th>
<th>Name</th>
<th>E-mail</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = 'SELECT * FROM Users ';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td>'.$row['UID'].'</td>';
echo '<td class="center">'.$row['Fuid'].'</td>';
echo '<td class="center">'.$row['Ffname'].'</td>';
echo '<td class="center">'.$row['Femail'].'</td>';
echo '<td>'.'<a href="#" class="btn btn-info btn-setting" >'.'Delete'.'</a>'.'</td>';
echo '</tr>';
}
?>
</tr>
</tbody>
</table>
<form action="db_sql/db_delete_supplier.php" method="post" name="frm1" id="frm1" >
<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">×</button>
<h3>Delete Supplier</h3>
</div>
<div class="modal-body">
<p>Are you sure to delete this supplier from system ...? </p>
</div>
<div class="modal-footer">
<input type="text" name="UID" value="<?php echo $row['UID'];?>" />
<!--Close
Delete-->
<input type="submit" class="btn btn-default" name="no" value="Close" />
<input type="submit" class="btn btn-primary" name="yes" value="Delete"/>
</div>
</div>
</div>
</div>
</form>
<?php echo "Edit"; ?>
Put this code in While loop , so this will open pop-up windows with query-string of cat-id.
Now from that ID you can fetch all records in pop-up.
This is possible solution you can do....
Note : you can pass any ID which is unique so that you can execute select query on pop-up page and it will fetch data from database