Bootstrap modal on each individual row - php

Basically, the page is supposed to prompt the user (preferably with a modal) before confirming the action.I modified our web page so it would include modals on delete per individual row. The modal doesn't seem to open if I include id="modalname" on both the data-target button and on the modal id.
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Full Name</th>
<th>Company</th>
<th>Address</th>
<th>Email</th>
<th>Contact No.</th>
<th>Birth Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query=mysqli_query($con,"select * from client order by lname")or die(mysqli_error());
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['lname'];?>,
<?php echo $row['fname'];?> <?php echo $row['mname'];?> </td>
<td><?php echo $row['company'];?></td>
<td><?php echo $row['address'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td>
<td><?php echo $row['bday'];?></td>
<td>
<button type="button" data-toggle="modal" data-target="#deletemodale<?php echo $row['id'];?>" >Open Modal</button>
</td>
</tr>
</tbody>
</table>
Here's a sample code for my table. It includes the echoed values from the database with a button on each row for the modal.
Modal Code:
<div class="modal fade" id="deletemodale<?php echo $row['id'];?>" 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">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I tried this with tag and href earlier, the row id seems to be showing on the url if I hover on it but the modal won't open on both cases. The modal also seems to open properly if I remove the php tag on its id. I also only have this modal code for the rest of the rows because of its dynamic nature

From what i see you code is valid, but i dont see where your while loop is ending. If you dont have your modal code inside your while loop only one modal html code will be printed, so there will be a modal only for the last row['id'].Is that the case? Also your while loop should be ending before the tbody tag.

I took the liberty and added closing brace for your while loop just after the last </tr> (before the </tbody> in your first piece of code. I also copied the second piece of your code (you named it "Modal Code") right before that closing brace – since you give different id’s per modal, I assume that each row in your table has own modal. And everything works fine on my WAMP server.
Make sure that:
You have included jquery.js, bootstrap.css and bootstrap.js
You have enabled javascript in your browser.

Related

How to edit users from a database in a table format

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>";

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'));
});

Data modal dialog does not show up

i have created a button that will view the vehicles details when clicked. how ever, my data modal dialog does not show up. here is my codes..
<table class="table table-striped">
<tr>
<th width="40%">Plate Number</th>
<th width="30%">Type</th>
<th width="30%">View</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["plateNo_vehicle"]; ?></td>
<td><?php echo $row["vehicle_Type"];?></td>
<td><input type="button" name="view" value="view" id="<?php echo $row["id_vehicle"]; ?>" class="btn btn-info btn-xs view_data" /></td>
</tr>
<?php
}
?>
</table>
this is the modal class and the script. the script supposed to contain ajax that supposed to view all the vehicle detail but i changed it just to pop up a data modal dialog only because the data modal wont show up at all.
<div id="dataModal" 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">Vehicles Details</h4>
</div>
<div class="modal-body" id="vehicle_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
script
<script>
$(document).ready(function(){
$('.view_data').click(function(){
$('#dataModal').modal("show");
});
});
It seems like you're loading jQuery twice.
First instance: <script src="../vendor/jquery/jquery.min.js"></script>
Second instance: <script src="ajax.googleapis.com/ajax/libs/jquery/2.2.0/…
this may be causing conflicts. Stick with the latest version only.
If you are using Bootstrap you can just add:
data-toggle="modal" data-target="#dataModal"
attributes in your button

Passing value to Bootstrap Modal for PHP PDO Query

I am displaying a table of values with two options, delete or update. When I select delete I want to display the row to be deleted. This last point using Bootstrap Modals.
This is my Delete Modal
<div class="modal bounceIn animated" id="Delete" 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">Delete a Campaign</h4>
</div>
<div class="modal-body edit-content">
<p>Please find the campaign ID to be deleted below</p>
<input type="text" name="bookId" id="bookId" value="" />
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Description</th>
<th>Creation Date</th>
<th>Credit</th>
<th>ROI</th>
<th>Active</th>
</tr>
<?php
$id = $row['data-campaignid'];// This is where I am having the problem
$stmt = $user_home->runQuery("SELECT * FROM campaigns WHERE id_campaigns=:id");
$stmt->execute(array(":id"=>$id));
while($row=$stmt->fetch(PDO::FETCH_BOTH))
{
?>
<tr>
<th>
<?php print($row['id_campaigns']); ?>
</th>
<th>
<?php print($row['descripcion']); ?>
</th>
<th>
<?php print($row['fec_creacion']); ?>
</th>
<th>
<?php print($row['saldo']); ?>
</th>
<th>
<?php print($row['ROI']); ?>
</th>
<th>
<?php print($row['active']); ?>
</th>
</tr>
<?php
}
?>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<!-- modal-content -->
</div>
<!-- modal-dialog -->
</div>
<!-- end -->
So as you can see I am passing a value "bookid" to the modal using the following JQuery script
$(document).on("click", ".open-DeleteDialog", function () {
var myCampaignId = $(this).data('campaignid');
$(".modal-body #bookId").val( myCampaignId );
});
The difficulty I am encountering is how to use the value of the input (bookID) to pass it over to the PHP PDO Query and execute it while being able to show that data on the table.
JQuery is executed on client side while SQL queries are on server side.
So, you need to use AJAX. This would be a very long answer, please take a look at this CRUD+AJAX tutorial.

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