How to add while loop variable to bootstrap modal php - php

I have a table in a while loop that includes a edit button to a modal. What I am trying to achieve is when you click the modal button "edit" the data in the modal will correspond with the correct teacher name. So I am trying to loop the modals and the data in it. What I have done is not working, it is only showing either the first or last value in the array for all modals in the loop.
so the table looks like this
| teacher name | Phone | Email | Edit | Delete |
| Mary | 111-111-1111 | mary#mary.com | Edit | Delete |
| Rob | 111-111-1111 | rob#rob.com | Edit | Delete |
| Liz | 111-111-1111 | Liz#Liz.com | Edit | Delete |
When I click on any of the edits the teacher name "Mary" shows up for all of them. But what I want is for Mary to show for the 1st edit, Rob to show for the 2nd edit, and so forth...
CODE:
<!--TABLES-->
<div id="table" class="table-responsive-sm container">
<table id="feisHistory" class="table table-striped table-bordered responsive table-hover" cellspacing="0" style="width:100%">
<thead>
<tr>
<th id="teacherName" >Teacher Name</th>
<th id="phone">Phone</th>
<th id="email">Email</th>
<th id="edit" data-feild="operate" data-events="operateEvents" data-formatter="operateFormatter" class="d-print-none">Edit</th>
<th id="delete" data-feild="operate" data-events="operateEvents" data-formatter="operateFormatter" class="d-print-none">Delete</th>
</tr>
</thead>
<tbody>
<?php
$school_sql = "SELECT * FROM `Users` WHERE id = '$id'";
$school_res = mysqli_query($con,$school_sql);
$school_row = mysqli_fetch_array($school_res);
$school_name = $school_row["school"];
$sql = "SELECT * FROM `teachers` WHERE school = '$school_name' ORDER BY teacher ASC";
$res = mysqli_query($con,$sql);
if($res==FALSE){
die('there was an error running query [' . $con->error . ']');
}
while($row=mysqli_fetch_array($res)){
$teacherName = $row["teacher"];
$teacherPhone = $row["phone"];
$teacherEmail = $row["email"];
$teacherId = $row["id"];
?>
<tr>
<td><?php echo $teacherName; ?></td>
<td><?php echo $teacherPhone; ?></td>
<td><?php echo $teacherEmail; ?></td>
<td class="d-print-none" style="margin: auto;">
Edit
<!---MODAL BUTTON-->
<div class="col-sm-12 content-btn">
<div class="row">
<div class="col-sm-12">
<div class="formModal1">
<div class="modal fade bd-editprofile-modal-lg" id="editProfile" tabindex="-1" role="dialog" aria-labelledby="editProfile" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-success modal-lg" role="document">
<div class="modal-content">
<div class="modal-header" style="color: #464646">
<h5 class="modal-title text-center">Edit Teacher</h5>
</div>
<form id="updateForm" method="post" class="form-horizontal" name="tcol" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" role="form" data-toggle="validator">
<input type="hidden" name="id" value="">
<div class="controls">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-3">
<label> Teacher Name</label>
</div>
<div class="col-sm-8">
<input id="teacher_name" type="text" name="teacher_name" class="form-control" placeholder="<?php echo $teacherName?>">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- MODAL FOOTER -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save" id="save">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="d-print-none" style="margin: auto;">
Delete
</td>
</tr>
<?php } ?>
</tbody>
</table>
I know my code is sloppy, please don't criticize me too much.. I'm still learning.

You could use the non-ajax version, but you should avoid this since it is rewriting large amount of code (depends on the records), by assigning unique id on each of elements id within the while loop, maybe you could use $teacherId as an identifier :
while($row=mysqli_fetch_array($res)){
$teacherName = $row["teacher"];
$teacherPhone = $row["phone"];
$teacherEmail = $row["email"];
$teacherId = $row["id"];
?>
<tr>
<td><?php echo $teacherName; ?></td>
<td><?php echo $teacherPhone; ?></td>
<td><?php echo $teacherEmail; ?></td>
<td class="d-print-none" style="margin: auto;">
Edit
<!---MODAL BUTTON-->
<div class="col-sm-12 content-btn">
<div class="row">
<div class="col-sm-12">
<div class="formModal1">
<div class="modal fade bd-editprofile-modal-lg" id="editProfile<?=$teacherId?>" tabindex="-1" role="dialog" aria-labelledby="editProfile" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-success modal-lg" role="document">
<div class="modal-content">
<div class="modal-header" style="color: #464646">
<h5 class="modal-title text-center">Edit Teacher</h5>
</div>
<form id="updateForm<?=$teacherId?>" method="post" class="form-horizontal" name="tcol" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" role="form" data-toggle="validator">
<input type="hidden" name="id" value="">
<div class="controls">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-3">
<label> Teacher Name</label>
</div>
<div class="col-sm-8">
<input id="teacher_name<?=$teacherId?>" type="text" name="teacher_name" class="form-control" placeholder="<?php echo $teacherName?>">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- MODAL FOOTER -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save" id="save<?=$teacherId?>">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="d-print-none" style="margin: auto;">
Delete
</td>
</tr>
<?php } ?>

You can solve this with many ways, and i use ajax for this solution.
And this one example how to use ajax to get data for each teacher name.
$('.edit').on('click', function(){
let id = $(this).attr("data-id");
let tempurl = "";
// this is example cause i create seperate json data, you can dynamically create select your DB based on id
if (id == 1){
tempurl = "https://api.myjson.com/bins/19w0nm";
}else if (id == 2){
tempurl = "https://api.myjson.com/bins/rhq2a";
}else if (id == 3){
tempurl = "https://api.myjson.com/bins/m4tma";
}
$.ajax({
url: tempurl,
type: "get",
dataType: "JSON",
data: {}, //this is data you send to your server
success: function(res)
{
console.log(res);
$('#tchName').val(res.name);
$('#tchPhone').val(res.phone);
$('#tchEmail').val(res.email);
$("#modalEdit").modal('show');
}
})
})
$('#btnSave').on('click', function(e){
e.preventDefault();
$("#modalEdit").modal('toggle');
alert('Your data has been update');
})
<html>
<head>
<title>SO</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="modal" tabindex="-1" role="dialog" id="modalEdit">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal Edit</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<label for="">Name</label>
<input type="text" name="tchName" id="tchName" value="">
<br>
<label for="">Phone</label>
<input type="text" name="tchPhone" id="tchPhone" value="">
<br>
<label for="">Email</label>
<input type="text" name="tchEmail" id="tchEmail" value="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="btnSave">Save changes</button>
</div>
</div>
</div>
</div>
<h3>Testing Table</h3>
<div class="col-md-6">
<table class="table table-bordered" style="text-align:center">
<thead>
<td>Name</td>
<td>Phone</td>
<td>Email</td>
<td>Action</td>
</thead>
<tr>
<td><?php echo 'Teacher Name 1'; ?></td>
<td><?php echo '0211'; ?></td>
<td><?php echo 'email1#email.com'; ?></td>
<td class="d-print-none" style="margin: auto;">
<button data-toggle="modal" class="edit" data-id="1">Edit</button>
</tr>
<tr>
<td><?php echo 'Teacher Name 2'; ?></td>
<td><?php echo '0222'; ?></td>
<td><?php echo 'email2#email.com'; ?></td>
<td class="d-print-none" style="margin: auto;">
<button data-toggle="modal" class="edit" data-id="2">Edit</button>
</tr>
<tr>
<td><?php echo 'Teacher Name 3'; ?></td>
<td><?php echo '0223'; ?></td>
<td><?php echo 'email3#email.com'; ?></td>
<td class="d-print-none" style="margin: auto;">
<button data-toggle="modal" class="edit" data-id="3">Edit</button>
</tr>
</table>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
ignore the tempurl, you can create function to select and get specific data of your teacher based on teacher name, id or anything else.

Related

Show Detail of Data in Bootstrap Modal based on ID

I have some edit modal and i want it to show the detail of DATA in bootsrap modal based on ID. Here is my index code:
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800"><?= $title; ?> </h1>
<div class="row">
<div class="col-lg-6">
<?= form_error('menu', '<div class="alert alert-danger" role="alert">','</div>'); ?>
<?= $this->session->flashdata('message'); ?>
Tambah User
<table class="table table-hover">
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Nama</th>
<th scope="col">Email</th>
<th scope="col">Password</th>
<th scope="col">Role</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($userRole as $u) : ?>
<tr>
<th scope="row"><?= $i ; ?></th>
<td><?= $u['name']; ?> </td>
<td><?= $u['email']; ?> </td>
<td><?= $u['password']; ?> </td>
<td><?= $u['role']; ?> </td>
<td>
<a href="" class="badge badge-success" data-toggle="modal"
data-target="#editRoleModal">edit</a>
<a href="<?= base_url('admin/deleteuser/') . $u['id'];?>"
class="badge badge-danger">delete</a>
</td>
</tr>
<?php $i++ ; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
And here is my modal :
<!-- Modal Edit-->
<div class="modal fade" id="editRoleModal" 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">Edit User</h5>
<button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
</div>
<?= form_open_multipart('admin/usermanagement'); ?>
<div class="modal-body">
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="text" class="form-control" id="name" name="name" value="<?= $user['name']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="email" class="form-control" id="email" name="email" value="<?= $user['email']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="text" class="form-control" id="password" name="password"
value="<?= $user['password']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<select name="menu_id" id="menu_id" class="form-control">
<option value="">Select Role</option>
<?php foreach ($user_role as $u) : ?>
<option value="<?= $u['id']; ?>"><?= $u['role']; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
For now It shows the data based on ID, but when i try to click edit button of another data, it show the same detail of one ID only.
I hope that every i click every data button, it shows different detail based on id. I've check similar case already but no one of them that work on my code. I just know my code need some jquery, but i just new on it.
like saad said, place it inside the loop
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800"><?= $title; ?> </h1>
<div class="row">
<div class="col-lg-6">
<?= form_error('menu', '<div class="alert alert-danger" role="alert">','</div>'); ?>
<?= $this->session->flashdata('message'); ?>
Tambah User
<table class="table table-hover">
<thead>
<tr>
<th scope="col">No</th>
<th scope="col">Nama</th>
<th scope="col">Email</th>
<th scope="col">Password</th>
<th scope="col">Role</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($userRole as $u) : ?>
<tr>
<th scope="row"><?= $i ; ?></th>
<td><?= $u['name']; ?> </td>
<td><?= $u['email']; ?> </td>
<td><?= $u['password']; ?> </td>
<td><?= $u['role']; ?> </td>
<td>
<a href="" class="badge badge-success" data-toggle="modal"
data-target="#editRoleModal<?=$i?>">edit</a>
<a href="<?= base_url('admin/deleteuser/') . $u['id'];?>"
class="badge badge-danger">delete</a>
<!-- Modal Edit-->
<div class="modal fade" id="editRoleModal<?=$i?>" 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">Edit User</h5>
<button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>
</div>
<?= form_open_multipart('admin/usermanagement'); ?>
<div class="modal-body">
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="text" class="form-control" id="name" name="name" value="<?= $u['name']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="email" class="form-control" id="email" name="email" value="<?= $u['email']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<input type="text" class="form-control" id="password" name="password"
value="<?= $u['password']; ?>">
</div>
<div class="mb-3">
<!-- <label for="exampleInputPassword1" class="form-label">Password</label> -->
<select name="menu_id" id="menu_id" class="form-control">
<option value="">Select Role</option>
<?php foreach ($user_role as $u2) : ?>
<option value="<?= $u2['id']; ?>"><?= $u2['role']; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>
<?php $i++ ; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>

Bootstrap Modal not working for all records in table

I have made a bootstrap modal for updating each record in the table individually using a while loop in PHP, but it seems to work only for the first record of the table. I'm not being able to figure out where I'm going wrong. Please help me with it let me know where I'm going wrong.
Here's the code:
<!DOCTYPE html>
<html>
<body>
<?php
$sql = "SELECT * FROM fee_status";
$result = $conn->query($sql);
$i = 1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<table>
<tr>
<td width='5%'><?php echo $i++; ?></td>
<td width='20%'><?php echo $row["student_id"]; ?><br><?php echo $row["student_name"]; ?></td>
<td width='10%'><?php echo $row["fee_due"]; ?></td>
<td width='25%'>
<div class="dropdown">
<button class='btn btn-primary dropdown-toggle' type='button' id='dropdownMenuButton' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>Actions</button>
<div class='dropdown-menu' aria-labelledby='dropdownMenuButton'>
<a class='dropdown-item' data-toggle='modal' data-target="#myModal<?php echo $row['student_id']; ?>">Update Payment</a>
</div>
</div>
</td>
</tr>
</table>
<div class="modal" role="dialog" id="myModal<?php echo $row['student_id']; ?>">
<div class="modal-dialog" role="document" style="max-width: 65%;">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Update Payment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="update_payment_form" class="form-group" action="update_fee.php" method="post">
<div class='form-group required'>
<div class="row">
<div class="col-md-6">
Student Details: <input type="text" name="student_details" value='<?php echo $row['student_id']; ?> - <?php echo $row['student_name']; ?>' style='width: 70%;' readonly>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
Amount: <input type="number" name="new_payment" placeholder='<?php echo $row['fee_due']; ?>' style='width: 40%;' required>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
Date: <input id="date-field" type="date" name="date" style='width: 40%;'>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" onclick="form_submit()" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
<?php
}
}
?>
</body>
</html>

Can I edit bootstrap modal without using JS/Ajax?

I am basically performing CRUD operation, in which I'm adding the data from modal which successfully added as well as fetching successfully but I want to edit there is a problem to get the id. I want to do this without using JS/AJAX, is it possible to edit the record in a bootstrap modal?
list page start, where data fetching from DB
<div class="table-responsive table-responsive-data2">
<table class="table table-data2">
<thead>
<tr class="bg bg-success">
<th class="text-center">Sr#</th>
<th class="text-center">Room Name</th>
<th class="text-center">Status</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>';
//-----------------------------------------------------
$sql = $conn->query("SELECT * FROM rooms");
$srno = 0;
//-----------------------------------------------------
while($values = mysqli_fetch_array($sql)) {
//-----------------------------------------------------
$srno++;
//-----------------------------------------------------
echo '
<tr class="tr-shadow">
<td>'.$srno.'</td>
<td>'.$values['room_name'].'</td>
<td>';
if($values['room_status'] == 1)
{
echo'<span class="status bg bg-info">Active</span>';
}
else if($values['room_status'] == 2){
echo'<span class="status bg bg-danger">Inactive</span>';
}
echo "id:".$id = $values['room_id'];
echo'
</td>
<td>
<input type="text" name="id" value="'.$values['room_id'].'">
<div class="table-data-feature">
<button class="item" data-toggle="modal" data-target="#update_room" data-id="'.$values['room_id'].'">
<i class="zmdi zmdi-edit"></i>
</button>
</div>
</td>
</tr>
<tr class="spacer"></tr>';
}
echo'
</tbody>
</table>
</div>
list page end
Modal start, to edit the record
<?php
//-----------------------------------------------------
$sql = $conn->query("SELECT * FROM rooms WHERE room_id ='".$_GET['id']."'");
$srno = 0;
//-----------------------------------------------------
($values = mysqli_fetch_array($sql));
echo'
<div class="modal fade" id="update_room" tabindex="-1" role="dialog" aria-labelledby="mediumModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header bg bg-success">
<h4 class="modal-title text-white" id="mediumModalLabel"><i class="fa fa-plus"></i> Add Room </h4>
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close" >
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body mt-lg">
<form action="#" class="form-horizontal" id="form" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<input type="text" name="room_id" id="room_id" value="id:'.$_GET['id'].'">
<div class="form-group">
<div class="col-md-12">
<label class="control-label">Room Name <span class="required">*</span></label>
<input type="text" class="form-control" required name="room_name" value="'.$values['room_name'].'"/>
</div>
</div>
<div class="form-group" >
<div class="col-md-12">
<label class="control-label">Status <span class="required">*</span></label>
<div class="radio-inline">
<input type="radio" name="room_status" value="1" ';
if($values['room_status'] == 1)
{echo'checked'; }echo'>
<label for="">Active</label>
<input type="radio" name="room_status" value="2" '; if($values['room_status'] == 2) {echo'checked'; }echo'>
<label for="">Inactive</label>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" name="submit_room">Submit</button>
</div>
</form>
</div>
</div>
</div>
';
?>
modal end
Well, If you don't want to use ajax you have to submit a form to your bootstrap modal data Into a page and get them with get or post method and include them in php.
Like THis:
list page:
<form method="post" action="modal.php">
<!-- Your List page code --->
<!-- Input for your data --->
<input name="data">
<!-- a submit button --->
<button type="submit">submit</buttton>
</form>
modal Page:
//get data from list page
$data = $_POST["data"];
//Your Codes and data in your modal body

undefined variable in modal

if i click on view button in my table it should open an modal form and
display all table values to view it.But i'm getting error as undefined
variable in the textbox inside of location,where i called only location
in the table.kindly help out me with how to get datas from dbs as php
code.thanks in advance.
UPDATE: I had updated my code.Kindly check it out,as i called ajax ,but modal box open with empty ,no informations loaded.kindly help it out.
<!-- Main content -->
<div class="main-content">
<h1 class="page-title">OUR POP DETAILS</h1>
<!-- Breadcrumb -->
<ol class="breadcrumb breadcrumb-2">
<li><i class="fa fa-home"></i>Home</li>
<li>Metro Pop</li>
<li class="active"><strong>Action</strong></li>
</ol>
<div class="row">
<div class="col-md-12"></div>
<div class="col-md-4">
<?php $apage = array('id'=>'','name'=>'');?>
<script>var page_0 = <?php echo json_encode($apage)?></script>
<h3><a data="page_0" class="model_form btn btn-sm btn-danger" href="#"><span class="glyphicon glyphicon-plus"></span> Add new record</a></h3>
</div>
</div>
<div id="table-container">
<div class="row">
<div class="col-md-12">
<table id="table" class="table table-striped table-sortable table-condensed " cellspacing="0" width="100%"
data-show-columns="true"
>
<tbody>
<?php if(isset($result) && ($data_record) > 0) : $i=1; ?>
<?php while ($users = mysqli_fetch_object($result)) { ?>
<tr class="<?=$users->id?>_del">
<td><?=$i;?></td>
<td><?=$users->zonee;?></td>
<td><?=$users->location;?></td>
<td><?=$users->pop_type;?></td>
<td><?=$users->switch_name;?></td>
<td><?=$users->switch_ip;?></td>
<td><?=$users->switch_make;?></td>
<td><?=$users->switch_serial;?></td>
<td><?=$users->switch_model;?></td>
<td> <i class="material-icons"></i></td>
<script>var page_<?php echo $users->id ?> = <?php echo json_encode($users);?></script>
<td><a data="<?php echo 'page_'.$users->id ?>" class="model_form btn btn-info btn-sm" href="#"> <span class="glyphicon glyphicon-pencil"></span></a>
<a data="<?php echo $users->id ?>" title="Delete <?php echo $users->name;?>" class="tip delete_check btn btn-info btn-sm "><span class="glyphicon glyphicon-remove"></span> </a>
<button data-toggle="modal" data-target="#view-modal" data-id="<?php echo $users->id; ?>" class=" view_check btn btn-sm btn-info"><i class="glyphicon glyphicon-eye-open"></i></button>
</td>
</tr>
<?php $i++;
} ?>
<?php else : echo '<tr><td colspan="8"><div align="center">-------No record found -----</div></td></tr>'; ?>
<?php endif; ?>
</tbody>
</table>
<?php
if(isset($_SESSION['flash_msg'])) :
$message = $_SESSION['flash_msg'];
echo $error= '<div class="alert alert-success" role="alert">
<span class="glyphicon glyphicon-envelope"></span> <strong>'.$message.'</strong> </div>';
unset($_SESSION['flash_msg']);
endif;
?>
</div>
</div>
</div>
<div id="view-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<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">
<i class="glyphicon glyphicon-user"></i> POP Information
</h4>
</div>
<div class="modal-body" id="employee_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Form modal -->
<div id="form_modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><i class="icon-paragraph-justify2"></i><span id="pop_title">ADD</span> POP INFORMATION</h4>
</div>
<!-- Form inside modal -->
<form method="post" action="add_edit.php" id="cat_form">
<div class="modal-body with-padding">
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>zonee :</label>
<input type="text" name="zonee" id="zonee" class="form-control required">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" class="form-control required">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>pop_type :</label>
<input type="text" name="pop_type" id="pop_type" class="form-control required number">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_name:</label>
<input type="text" name="switch_name" id="switch_name" class="form-control required number">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_ip :</label>
<input type="text" name="switch_ip" id="switch_ip" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_make :</label>
<input type="text" name="switch_make" id="switch_make" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_serial :</label>
<input type="text" name="switch_serial" id="switch_serial" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>switch_model :</label>
<input type="text" name="switch_model" id="switch_model" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>Latitude:</label>
<input type="text" name="latitude" id="latitude" class="form-control required" >
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>Longitude:</label>
<input type="text" name="longitude" id="longitude" class="form-control required" >
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
<span id="add">
<input type="hidden" name="id" value="" id="id">
<button type="submit" name="form_data" class="btn btn-primary">Submit</button>
</span>
</div>
</form>
</div>
</div>
</div>
<!-- /form modal -->
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click','.model_form',function(){
$('#form_modal').modal({
keyboard: false,
show:true,
backdrop:'static'
});
var data = eval($(this).attr('data'));
$('#id').val(data.id);
$('#zonee').val(data.zonee);
$('#location').val(data.location);
$('#pop_type').val(data.pop_type);
$('#switch_name').val(data.switch_name);
$('#switch_ip').val(data.switch_ip);
$('#switch_make').val(data.switch_make);
$('#switch_serial').val(data.switch_serial);
$('#switch_model').val(data.switch_model);
$('#latitude').val(data.latitude);
$('#longitude').val(data.longitude);
if(data.id!="")
$('#pop_title').html('Edit');
else
$('#pop_title').html('Add');
});
$(document).on('click','.delete_check',function(){
if(confirm("Are you sure to delete data")){
var current_element = $(this);
url = "add_edit.php";
$.ajax({
type:"POST",
url: url,
data: {ct_id:$(current_element).attr('data')},
success: function(data) { //location.reload();
$('.'+$(current_element).attr('data')+'_del').animate({ backgroundColor: "#003" }, "slow").animate({ opacity: "hide" }, "slow");
}
});
}
});
$(document).on('click', '.view_check', function(){
//$('#dataModal').modal();
var employee_id = $(this).attr("id");
$.ajax({
url:"view.php",
method:"POST",
data:{employee_id:employee_id},
success:function(data){
$('#employee_detail').html(data);
$('#view-modal').modal('show');
}
});
});
});
});
</script>
**view.php**
<?php
include("config.php");
if(isset($_POST["employee_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "", "mine");
$query = "SELECT * FROM user WHERE id = '".$_POST["employee_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_object($result))
{
$output .= '
<tr>
<td width="30%"><label>Name</label></td>
<td width="70%">'.$users["location"].'</td>
</tr>
<tr>
<td width="30%"><label>Address</label></td>
<td width="70%">'.$users["zonee"].'</td>
</tr>
<tr>
<td width="30%"><label>Gender</label></td>
<td width="70%">'.$users["pop_type"].'</td>
</tr>
';
}
$output .= '</table></div>';
echo $output;
}
?>
Add quota('') like $row['location'] , you are using $row[location]
Or use below code
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" value="<?php
echo $row['location'];?>" />
</div>
Your variable name is $users not $row so you can write this
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label>location :</label>
<input type="text" name="location" id="location" value="<?php
echo $users['location'];?>" />
</div>
Try this Code
<tbody>
<?php if(isset($result) && ($data_record) > 0) : $i=1; ?>
<?php while ($users = mysqli_fetch_object($result)) { ?>
<tr class="<?=$users->id?>_del">
<td><?=$i;?></td>
<td><?=$users->zonee;?></td>
<td><?=$users->location;?></td>
<td><?=$users->pop_type;?></td>
<td><?=$users->switch_name;?></td>
<td><?=$users->switch_ip;?></td>
<td><?=$users->switch_make;?></td>
<td><?=$users->switch_serial;?></td>
<td><?=$users->switch_model;?></td>
<td> <a href="http://maps.google.com/?q=<?=$users-
>latitude;?>,<?=$users->longitude;?>" target=\"_blank\"><i
class="material-icons"></i></a></td>
<script>var page_<?php echo $users->id ?> = <?php
echo json_encode($users);?></script>
<td><a data="<?php echo 'page_'.$users->id ?>"
class="model_form btn btn-info btn-sm" href="#"> <span
class="glyphicon glyphicon-pencil"></span></a>
<a data="<?php echo $users->id ?>" title="Delete <?
php echo $users->name;?>" class="tip delete_check btn btn-info
btn-sm "><span
class="glyphicon glyphicon-remove"></span> </a>
<button data-toggle="modal" data-target="#view-
modal" data-id="<?php echo $users->id; ?>" id="getUser"
class="btn btn-sm
btn-info"><i class="glyphicon glyphicon-eye-open"></i>
</button>
</td>
</tr>
<?php $i++;
echo "<div class='modal-body'>
<div id='dynamic-content'>
<div class='form-group'>
<div class='row'>
<div class='col-sm-12'>
<label>location :</label>
<input type='text' name='location' id='location' value='$users->location' />
</div>
</div>
</div>
</div>
</div> "
} ?>
<?php else : echo '<tr><td colspan="8"><div align="center">-------No
record found -----</div></td></tr>'; ?>
<?php endif; ?>
</tbody>
On Click Call Function
<script>
function launch_modal(id)
{
//Store id in variable
var newId = id;
//Ajax Start
$.ajax({
type: "POST",
url: "your_php_page.php",
//send id to php page
data: {theId:newId},
success: function(data){
//to display data in paragraph of Modal
$('.modal-body').html(data);
//to display modal
$('#myModal').modal("show");
},
});
}
</script>
your_php_page.php
<?php
$theId = $_POST['theId'];
if($theId){
$output = '';
$sql = $conn->query("select * from table where id = '$theId'");
$fetch = $sql->fetch_object();
//Append
$output .= '<table class="table table-bordered">
<tr>
<td>Name :</td>
<td>'.$fetch->name.'</td>
</tr>
<tr>
<td>Number :</td>
<td>'.$fetch->number.'</td>
</tr>
';
echo $output;
}
?>

Passing data from page to bootstrap modal using SQL & PHP

So as per title, im trying to pass show multiple data from database using sql on the bootstrap modal. The ID will be pass down from the link, how is it done? been finding multiple way but I still can't show the selected data;
So here is the trigger for the modal:
<?php while($row = mysqli_fetch_array($adm_query)){
$id = $row['admin_id']; ?>
<tr>
<td style="text-align:center"><?php echo $row['adm_name']; ?></td>
<td width="150" style="text-align:center"><?php echo $row['staff_no']; ?></td>
<td width="120" style="text-align:center"><?php echo $row['department']; ?></td>
<td width="138" style="text-align:center;">
<a data-toggle="modal" data-target="#myModal" data-id="<?php echo $row['admin_id']?>" class="btn btn-outline btn-info"><i class="fa fa-search-plus"></i></a>
</td>
<?php }?>
Then this is the modal content:
<!-- Modal -->
<div style="margin-top:5%;" class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<?php $sel_query=mysqli_query($conn, "select * from admin where admin_id='$idmodal'")or die(mysql_error($conn)); $selrow=mysqli_fetch_array($sel_query);?>
<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">Modal title</h4>
</div>
<div class="modal-body">
<div class="panel panel-info" style="text-align:center;">
<div class="panel-heading">
<h4>Staff Details</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Staff ID</label>
<p>
<?php echo $selrow[ 'staff_no']?>
</p>
</div>
<div class="form-group">
<label>Name</label>
<p>
<?php echo $selrow[ 'adm_name']?>
</p>
</div>
<div class="form-group">
<label>Services | Department</label>
<p>
<?php echo $selrow[ 'department']?>
</p>
</div>
</div>
<!-- /.col-lg-6 (nested) -->
<div class="col-lg-6">
<div class="form-group">
<label>Username</label>
<p>
<?php echo $selrow[ 'username']?>
</p>
</div>
<div class="form-group">
<label>Password</label>
<p>
<?php echo $selrow[ 'password']?>
</p>
</div>
<div class="form-group">
<label>Date</label>
<p>
<?php echo $selrow[ 'date_added']?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
The problem is nothing works, and i don't know where to start.
Appreciate for the help.
Create one class openModal in <a></a>. Use this class in <script></script> to get data-id
<?php while($row = mysqli_fetch_array($adm_query,MYSQLI_ASSOC)){
$id = $row['admin_id']; ?>
<tr>
<td style="text-align:center"><?php echo $row['adm_name']; ?></td>
<td width="150" style="text-align:center"><?php echo $row['staff_no']; ?></td>
<td width="120" style="text-align:center"><?php echo $row['department']; ?></td>
<td width="138" style="text-align:center;">
<a class="btn btn-outline btn-info openModal" data-toggle="modal" data-target="#myModal" data-id="<?php echo $row['admin_id']?>">
<i class="fa fa-search-plus"></i>
</a>
</td>
</tr>
<?php }?>
Place this code in the same page below.
<div style="margin-top:5%;" class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content"></div>
</div>
</div>
JS (data-id=.. is passed here.)
<script>
$('.openModal').click(function(){
var id = $(this).attr('data-id');
$.ajax({url:"ajax_modal.php?id="+id,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
ajax_modal.php (Create one page in same directory ajax_modal.php. If you are looking to change this page name. Change in <script></script> tag too. Both are related.)
<?php
// Get `id` from `<script></script>`
$id = $_GET['id'];
$sel_query=mysqli_query($conn, "select * from admin where admin_id='$id'") or die(mysql_error($conn));
$selrow=mysqli_fetch_array($sel_query,MYSQLI_ASSOC);
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<div class="panel panel-info" style="text-align:center;">
<div class="panel-heading">
<h4>Staff Details</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Staff ID</label>
<p>
<?php echo $selrow[ 'staff_no']?>
</p>
</div>
<div class="form-group">
<label>Name</label>
<p>
<?php echo $selrow[ 'adm_name']?>
</p>
</div>
<div class="form-group">
<label>Services | Department</label>
<p>
<?php echo $selrow[ 'department']?>
</p>
</div>
</div>
<!-- /.col-lg-6 (nested) -->
<div class="col-lg-6">
<div class="form-group">
<label>Username</label>
<p>
<?php echo $selrow[ 'username']?>
</p>
</div>
<div class="form-group">
<label>Password</label>
<p>
<?php echo $selrow[ 'password']?>
</p>
</div>
<div class="form-group">
<label>Date</label>
<p>
<?php echo $selrow[ 'date_added']?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
For more info, click here
how-to-pass-current-row-value-in-modal
passing-data-via-modal-bootstrap-and-getting-php-variable
bootstrap-modal-and-passing-value
show-data-based-of-selected-id-on-modal-popup-window-after-click-a-button-php-my
Write below line in your code:-
$selrow=mysqli_fetch_assoc($sel_query);
OR
$selrow=mysqli_fetch_array($sel_query,MYSQLI_ASSOC);
instead of
$selrow=mysqli_fetch_array($sel_query);
Also it is bad practice to write query directly into modal.
You should use AJAX on click event. Also you should fill form data via jQuery OR javascript.

Categories