CRUD delete using modal form - php

I am creating a CRUD using PHP extension with PDO, and I want to create a confirmation for registry deletion using Bootstrap modals, but I don't know how I can pass the ID information to the modal to delete, how can I do it?
index.php
<tbody>
<?php
$pdo = DB::connect();
$stmt = $pdo->query("SELECT * FROM people");
while ($row = $stmt->fetch()) {
echo "<tr>";
echo "<th scope='row'>" . $row["id"] . "</th>";
echo "<td>" . $row["first_name"] . "</td>";
echo "<td>" . $row["last_name"] . "</td>";
echo "<td>" . $row["notes"] . "</td>";
if ($row["allowed"]) {
echo "<td>" . "✔" . "</td>";
} else {
echo "<td>" . "✗" . "</td>";
};
echo "<td>";
// modal trigger
echo "<button type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>";
echo "</td>";
echo "</tr>";
}
?>
</tbody>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Do you want to delete this registry?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Yes</button>
</div>
</div>
</div>
</div>

In this section add your ID
echo "<button type='button' class='btn btn-danger' data-toggle='modal' data-target='#exampleModal' onclick='$(".delete_id").val('".$row["id"]."')'>Delete</button>";
Then in modal add an hidden input field like so:
<input type="hidden" name="delete_id" class="delete_id">
Include jQuery before this function. You also need to have a delete file to pass that variable. After the request was success you could re-render the HTML.
function deleteRecord() {
var id = $(".delete_id").val();
$.ajax({
url:"/delete.php",
method:"POST",
data:{
id: id,
},
success:function(response) {
},
});
}
After we have stored the value of the ID we can delete it
<button type="submit" class="btn btn-danger" onclick="deleteRecord()">Yes</button>
Hope this was helpful

Related

Didn't get the ID from database based on row table

Why I can't get the ID from a row in table when I click Pending
Here is the code:
<!-- ############################################################################################################################################################################################################ -->
<!-- TABLE BUSINESS -->
<div class="row" id="TblBiz" style="display: none;">
<div class="col" style="height: 71px;">
<h1 style="font-family: Roboto, sans-serif; font-weight: 300;">Business</h1>
</div>
<div class="col-md-8 offset-md-2">
<div class="panel" id="main">
<div class="container-fluid container-bg">
<div class="table-responsive">
<table id="myTable4" class="table table-hover table-dark" style="font-size:11px; margin-top:6px;">
<thead style="font-size: 18px;">
<th>#</th>
<th>Business ID</th>
<th>Name</th>
<th>Owner</th>
<th>View</th>
<th>Approval</th>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM mechanic_business";
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
$i = 1;
while ($row = mysqli_fetch_assoc($result)) {
$biz_ID = $row['businessID'];
$mech_ID = $row['mechID'];
$mech_name = $row['mech_name'];
$bizName = $row['business_name'];
$bizApprove = $row['business_approval_status'];
$bizStatus = $row['business_status'];
// $dateJoin = $row['DATE_REGISTERED'];
// $dateFormated = date("d M Y", strtotime($dateJoin));
// Color class in PHP color(Pending, Approved, Banned)
$color = ($bizApprove === 'Pending') ? '#f0ad4e' : (($bizApprove === 'Approved') ? '#28a745' : '#dc3545');
echo "<tr style='font-size:18px;'>";
echo "<td class='align-middle'>" . $i; $i++ . "</td>";
echo "<td class='align-middle'>" . $biz_ID . "</td>";
echo "<td class='align-middle'>". $bizName ."</td>";
echo "<td class='align-middle'>". $mech_name ."</td>";
echo "<td class='align-middle'><input type='button' name='view' value='view' id='". $biz_ID ."' class='btn btn-info btn-xs view_biz'></td>";
echo "<td class='align-middle'><a role='button' class='approval-bton' id='". $biz_ID ."' style='color:". $color ."';>". $bizApprove ."</a></td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='7' class='table-active align-middle'>No Record in Database</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- ############################################################################################################################################################################################################ -->
<!-- ############################################################################################################################################ -->
<!-- UPDATE APPROVE STATUS MODAL -->
<div class="modal fade" id="approveModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"> Update Business Approval Status </h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="" method="post" enctype="multipart/form-data">
<div class="modal-body">
<input type="text" name="businessID" id="" value="<?php echo $biz_ID; ?>">
<div class="form-group">
<div>
<small id="photoHelp" class="form-text text-muted"><span style="color: #FF0000;">*</span><em>320px x 320px is ideal dimension, Format: jpg, png, jpeg, and PDF only</em></small>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="updateApprove" class="btn btn-primary">Save Change</button>
</div>
</form>
</div>
</div>
</div>
<!-- ############################################################################################################################################ -->
When I clicked a Pending from each row it suppose to get their ID based on the database but when I clicked from each row they display the same ID as shown picture in the link below.
https://drive.google.com/file/d/1IcfDUs3Q8Wl5vVO3KF793M5noTukLrCL/view?usp=sharing
They both show ORVA01BIZ-00000002 but in database first row is ORVA01BIZ-00000001 and ORVA01BIZ-00000002 is the second row and goes on.
There is very simple logic.
first, your model is not in loop so you can't pass $biz_ID in the model if you want to get each id from the database.
When the loop ends then it will only print the last row biz_id in the model.
The solution is :-
Changes you have to do in your code
You have to do one thing if you want to get each id
This is your second td :-
echo "<td class='align-middle bizzid'>" . $biz_ID . "</td>";
In your model you have to paste this : -
<input type="text" name="businessID" id="businessID" value="">-
This jquery code can help you to get desired result.
var bizid = $(this).closest('tr').find('.bizzid').text();
$('#businessID').val(bizid);
$('#approveModal').modal('show');
});'
Okay, got it
I did what you asked #Gurpreet Kait it really workss.
I put the jquery code inside the initialize modal bootstrap
// Show Modal 1
$(document).ready(function () {
$('.approval-bton').on('click', function() {
var bizid = $(this).closest('tr').find('.bizzid').text();
$('#businessID').val(bizid);
$('#approveModal').modal('show');
});
Thank you very much #Gurpreet Kait for your help.
});

Display Bootstrap Modal for Selected Table Item

I'm trying to create a simple buddy list for a web application. I'm looping through an SQL table to retrieve the current user's buddy list. Each row in the table has a button to display a modal of the selected user's ID. The current issue I'm having is that it's only showing the modal for the last user in the list, for everyone else it doesn't pop up. I need the modal to pop up for each user I click on and display that user's ID.
Example:
Here is the code I have so far:
HTML/PHP:
<?php
// Full SQL retrieval omitted above
$buddyUid = '';
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$buddyUid = $row['buddy_uid'];
echo "<tr>";
echo "<td> <input type='checkbox'></td>";
echo "<td>" . $buddyUid . "</td>";
echo "<td> <button type='button' onclick='javascript:selectBuddy($(this)); ' data-bs-toggle='modal' data-bs-target='#modal-" . $buddyUid . "'><i class='fas fa-share-square'></i></button></td>";
echo "</tr>";
}
?>
<!-- Modal -->
<div class="modal fade" id="modal-<?=$buddyUid?>" 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">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<?= $buddyUid?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Javascript:
function selectBuddy(buddy) {
$('#modal-' + buddy).modal('show');
}
$(this) refers to the element in the DOM to which the onclick attribute belongs:
in your case to get only buddy_id just change onclick event
from this
...onclick='javascript:selectBuddy($(this));'...
to this
...onclick='javascript:selectBuddy(".$buddyUid.");'...
also change your Modal id from
<div class="modal fade" id="modal-<?=$buddyUid?>" ...
to
<div class="modal fade" id="modal-buddy" ....
then javascript code like this
function selectBuddy(buddy) {
$('#modal-buddy').find('.modal-body').html(buddy);
$('#modal-buddy').modal('show');
}

Echo HTML tag problems with quotes

I am trying to put quotes in the php that handles the select tag. I need that the select tag displays the result of the query that is inside. How can I do this correctly?
<?php
$sql1 = mysql_query("SELECT * FROM `metlab`.`cutlog_junta` WHERE `finalizado` = 0")or die (mysql_error());
$idtimeleft= 0;
while($array = mysql_fetch_array($sql1, MYSQL_BOTH)){
$idtimeleft++;
$idprueba = $array[0];
$id_log = $array["id_log"];
$id_operador = $array["id_operador"];
$started = $array["started"];
$finished = $array["finished"];
date_default_timezone_set("America/Mexico_City");
$tiempo1 = new DateTime('now');
$tiempo2 = new DateTime($finished);
if ($tiempo1 < $tiempo2){
$tiemporestante = date_diff($tiempo2,$tiempo1 );
$timeleft = $tiemporestante->format("%h:%i:%s");
}else{
$timeleft = "00:00:00";
}
$sql = mysql_query("SELECT r.job AS JOB,p.heat_code AS HEAT_CODE,p.probeta AS PROBETA,p.id_line AS ID_LINE,p.id_box_pin AS ID_BOX_PIN,p.id_conexion AS ID_CONEXION
FROM metlab.prueba_junta p
INNER JOIN metlab.requerimientos_junta r ON p.id_job=r.id WHERE p.id='".$id_log."'")or die (mysql_error());
$mhs = mysql_fetch_array($sql);
echo (" <tr id='row". $idtimeleft ."'>
<td><input type='checkbox' name='record'></td>
<td>" . $id_log . "</td>
<td>" . $mhs['JOB'] . "</td>
<td>" . $mhs['ID_BOX_PIN'] . "</td>
<td>" . $mhs['ID_CONEXION'] . "</td>
<td>" . $mhs['HEAT_CODE'] . "</td>
<td>" . $mhs['PROBETA'] . "</td>
<td>" . $mhs['ID_LINE'] . "</td>
<td>" . $started . "</td>
<td><div id='idtimeleft" . $idtimeleft ."'>" . $timeleft . "</div></td>
<td><button type='button' class='btn btn-primary' data-toggle='modal' data-target='#cut_process'><i class='fa fa-cog fa-spin'></i>CUT</button>
<div id='cut_process' class='modal fade' role='dialog'>
<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'>CUT PROCESS</h4>
</div>
<div class='modal-body'>
<input type='text' placeholder='INSERT COMMENT' class='form-control'><br>
<label>Supervisor:</label>
<select class='btn btn-primary'>
<?php //////////////////////////////problem
$query='SELECT nombre
FROM metlab.supervisores';
$result1=mysql_query($query);
while($row1=mysql_fetch_array($result1)):;?>
<option><?php echo $row1[0];?>
</option>
<?php endwhile;?>
</select> /////////////////////////////end problem
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-success' data-dismiss='modal'><i class='fa fa-paper-plane'></i>SEND COMMENT</button>
</div>
</div>
</div>
</div>
</td>
<td>
<button type='button' class='btn btn-warning' data-toggle='modal' data-target='#milling_process'><i class='fa fa-cog fa-spin'></i>MILLING</button>
<div id='milling_process' class='modal fade' role='dialog'>
<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'>MILLING PROCESS</h4>
</div>
<div class='modal-body'>
<input type='text' placeholder='INSERT COMMENT' class='form-control'>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-success' data-dismiss='modal'><i class='fa fa-paper-plane'></i>SEND COMMENT</button>
</div>
</div>
</div>
</div>
</td>
<td>
<button type='button' class='btn btn-success' data-toggle='modal' data-target='#rectified_process'><i class='fa fa-cog fa-spin'></i>RECTIFIED</button>
<div id='rectified_process' class='modal fade' role='dialog'>
<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'>RECTIFIED PROCESS</h4>
</div>
<div class='modal-body'>
<input type='text' placeholder='INSERT COMMENT' class='form-control'>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-success' data-dismiss='modal'><i class='fa fa-paper-plane'></i>SEND COMMENT</button>
</div>
</div>
</div>
</div>
</td>
<td>
<button type='button' class='btn btn-danger' data-toggle='modal' data-target='#machined_process'><i class='fa fa-cog fa-spin'></i>MACHINED</button>
<div id='machined_process' class='modal fade' role='dialog'>
<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'>MACHINED PROCESS</h4>
</div>
<div class='modal-body'>
<input type='text' placeholder='INSERT COMMENT' class='form-control'>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-success' data-dismiss='modal'><i class='fa fa-paper-plane'></i>SEND COMMENT</button>
</div>
</div>
</div>
</div>
</td>
<td>" . $finished . "</td>
<td>" . $idprueba . "</td>
</tr>");
}
?>
I think you should write directly HTML code inside the page avoiding PHP echo function.
In this example you use PHP short tag only when you need it:
<tr id="row<?=$idtimeleft?>">
And when you need to perform some action you just open PHP tag and write your code.
<select>
<?php
$query='SELECT nombre FROM metlab.supervisores';
$result1=mysql_query($query);
while($row1=mysql_fetch_array($result1)):
?>
<option><?=$row1[0]?></option>
<?php endwhile;?>
</select>
You don't need semicolon after "while(...):"
Anyway you should read something about Twig to use template engine to write cleaner code.
Bye!!

Update & delete MySQL from modal bootstrap

I´m trying to delete and/or update data from form inside a modal window .For create it I haven´t problem but with the others actions (delete and update) I always have a problem,I think the key is the "id" it seems take the last value and not the value that I want to delete or update.
I know my code could be more nice.
<div class="container">
<div class="row espacio">
<p class="col-md-1"><a data-toggle="modal" href="#myModal" class="btn btn-default glyphicon glyphicon-plus"><span></span></a></p>
<p class="col-md-1 col-md-offset-10"><span class="btn btn-default glyphicon glyphicon-envelope"></span></p>
<table class="table table-bordered">
<caption>Bitácora</caption>
<thead>
<tr>
<th>Tecnico</th>
<th>Turno</th>
<th>Información</th>
<th>Estado</th>
<th>Creada</th>
<th>Modificada</th>
</tr>
</thead>
<tbody>
<?php
$txt ="";
$nombres =array();
$turnos = array('Mañana','Tarde','Noche');
$estados =array('Pendiente','Cerrado');
$db = new Db;
$sql = "SELECT * FROM tecnicos";
foreach ($db->consulta($sql) as $row) {
array_push($nombres,$row['Nombre']);
}
$sql = 'SELECT * FROM bitacora ORDER BY Estado DESC';
$consulta = $db->consulta($sql);
while ($row = mysqli_fetch_array($consulta)) {
$tecnico = $turno = $estado = "";
if ($row['Estado'] == "Pendiente"){
$txt .= "<tr class= 'bg-warning'>";
}else{
$txt .= "<tr class= 'bg-success'>";
}
$txt .= "
<td>". $row['Name'] . "</td>
<td>". $row['Turno'] . "</td>
<td>". $row['Info'] . "</td>
<td>". $row['Estado'] . "</td>
<td>". $row['RecordDate'] . "</td>
<td>". $row['ModiDate'] . "</td>
<td class='text-center'><a data-toggle='modal' href='#myModalUpdate".$row['Id']."''><span class='glyphicon glyphicon-pencil'></span></a>
<a data-toggle='modal' href='#myModalDelete".$row['Id']."''><span class='glyphicon glyphicon-remove'></span></a> </td>
</tr>";
//MODAL UPDATE
for ($i=0; $i <sizeof($nombres) ; $i++) {
if ($nombres[$i] == $row['Name'] ) {
$tecnico .= '<option value="'.$nombres[$i].'" selected="selected">'.$nombres[$i].'</option>';
}
else{
$tecnico .= '<option value="'.$nombres[$i].'" >'.$nombres[$i].'</option>';
}
}
for ($i=0; $i <sizeof($turnos) ; $i++) {
if ($turnos[$i] == $row['Turno']) {
$turno .= '<option value="'.$turnos[$i].'" selected="selected">'.$turnos[$i].'</option>';
}
else{
$turno .= '<option value="'.$turnos[$i].'" >'.$turnos[$i].'</option>';
}
}
for ($i=0; $i <sizeof($estados) ; $i++) {
if ($estados[$i] == $row['Estado']) {
$estado .= '<option value="'.$estados[$i].'" selected="selected">'.$estados[$i].'</option>';
}
else{
$estado .= '<option value="'.$estados[$i].'" >'.$estados[$i].'</option>';
}
}
$txt .= " <div class='modal fade' id='myModalUpdate".$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'>Modifica registro en Bitácora</h4>
</div>
<div class='modal-body'>
<form role='form' action='' method='post'>
<div class='form-group'>
<input type='text' name='id' value='".$row['Id']."'>
</div>
<div class='form-group'>
<label>Técnico</label>
<select class='form-control' name='modnombre'>
".$tecnico."
</select>
</div>
<div class='form-group'>
<label>Turno</label>
<select class='form-control' name='modturno'>
".$turno."
</select>
</div>
<div class='form-group'>
<label>Información</label>
<textarea class='form-control' name='modinformacion'>".$row['Info']."</textarea>
</div>
<div class='form-group'>
<label>Estado</label>
<select class='form-control' name='modestado'>
".$estado."
</select>
</div>
<div class='form-group text-center'>
<button type='submit' class='btn btn-warning' name='actualizar'>Actualiza</button>
</div>
</div><!--/modal body-->
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->";
//MODAL DELETE
$txt .= " <div class='modal fade' id='myModalDelete".$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'>Elemina registro en Bitácora</h4>
</div>
<div class='modal-body'>
<form role='form' action='' method='post'>
<div class='form-group'>
<input type='text' name='id' value='".$row['Id']."'>
</div>
<div class='form-group text-center'>
<button type='submit' class='btn btn-danger' name='eliminar'>Elimina</button>
</div>
</div><!--/modal body-->
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->";
}//end for principal
echo $txt;
?>
</tbody>
</table>
</div>
<!--MODAL CREATE-->
<div class="modal fade" id="myModal" 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">Añade registro en Bitácora</h4>
</div>
<div class="modal-body">
<form role="form" action="" method="post">
<div class="form-group">
<label>Técnico</label>
<select class="form-control" name="nombre">
<?php
$txt ="";
$db = new Db;
$sql = "SELECT * FROM tecnicos";
foreach ($db->consulta($sql) as $row) {
$txt.= "<option value='".$row['Nombre']."'>".$row['Nombre']."</option>";
}
echo $txt;
?>
</select>
</div>
<div class="form-group">
<label>Turno</label>
<select class="form-control" name="turno">
<option value="Mañana">Mañana</option>
<option value="Tarde">Tarde</option>
<option value="Noche">Noche</option>
</select>
</div>
<div class="form-group">
<label>Información</label>
<textarea class="form-control" name="informacion"></textarea>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success" name="crear">Crear</button>
</div>
</form>
</div><!--/modal body-->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!--/modal content-->
</div><!--/modal dialog-->
</div><!--/modal-->
</div> <!-- /container -->
And these are the action´s form
enter code //formulario CREATE
if(isset($_POST["crear"])) {
$nombre = $_POST["nombre"];
$turno = $_POST["turno"];
$informacion = $_POST["informacion"];
$sql = "INSERT INTO bitacora (Name,Turno,Info,Estado,RecordDate,ModiDate) VALUES ('" . $nombre . "', '" . $turno . "', '" . $informacion . "','Pendiente',now(),now())";
$db->consulta($sql);
}
//formulario UPDATE
if(isset($_POST["actualizar"])) {
$sql = "UPDATE bitacora SET Name = '" . $_POST["modnombre"] . "', Turno = '" . $_POST["modturno"] . "', Info = '" .$_POST["modinformacion"]. "', Estado = '" . $_POST["modestado"] . "' , ModiDate =now() WHERE Id = '".$_POST["id"]."' ";
$db->consulta($sql);
}
//form DELETE
if(isset($_POST["eliminar"])) {
$id = $_POST["id"];
$sql = "DELETE FROM bitacora WHERE Id = $id";
$db->consulta($sql);
}here
It seems that your input named "id" is not taking the correct value of $row['Id']. Try to inspect it and see if his "value" is not empty.
If the value was empty, your problem is the value of $row['Id'].
Try to replace $row['Id'] by $row['id'] and see the results.

How to echo a value out from a button?

I have a page that have a button like this,
When this button is clicked, a pop out modal will show,
Once the submit button is clicked, a small note will show up,
Below are my codes,
<div class="col-md-12" role="main">
<div class="bs-docs-section">
<h1 class="page-header">Today's Activity <button class="btn btn-primary">Add new</button></h1>
</div>
<?php
$con = getDbConnect();
$day = date("l");
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM timetableschedule WHERE staffID= '" . $staff_information["staff_Id"] . "' AND day='" . $day . "' ");
while ($schedule = mysqli_fetch_array($result)) {
?>
<div class="col-md-3">
<button class="btn btn-warning" data-toggle="modal" data-target="#myModal" schedule="
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>">
<?php
echo "<br/>";
echo $schedule['academicInstitution'] . "<br />";
echo $schedule['startTime'] . "-" . $schedule['endTime'] . "hrs<br />";
echo "<br/>";
?>
</button>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Insert Today's Activity</h4>
</div>
<div class="modal-body">
<p class="activity"></p>
<p>Click on the submit button if the above infomation is correct.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary btn-ok SUB" >Submit</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#myModal').on('show.bs.modal', function (e) {
$(this).find('.btn-ok').attr('schedule', $(e.relatedTarget).attr('schedule'));
$('.activity').html('You have selected: <strong>' + $(this).find('.btn-ok').attr('schedule') + '</strong>');
});
$(document).ready(function(){
$(".SUB").click(clickSUB);
})
function clickSUB(){
$("#processSUB").load("hello.php");
}
</script>
<div id="processSUB"></div>
<?php
}
mysqli_close($con);
}
?>
</div>
My codes for the hello.php,
<?php
echo 'You have submitted';
echo $schedule['timetableId'];
?>
Is there a way to get the 'timetableId' from the button?
Check 2nd Parameter of load() to pass data as parameter, you can pass timetableId or any other parameter required.
load('hello.php', { timetableId : javascriptVarWithValue });
you will receive passed data in $_POST in hello.php
In hello.php you can access like:
<?php echo $_POST['timetableId']; ?>
In loop, load the timetableId something like:
<div id="tableID" data-timetable-id="<?php $schedule['timetableId']; ?>" style="display: none;"></div>
In clickSUB() fetch the value something like this:
function clickSUB(){
// changed to data('timetableId') instead of data('timeTableId')
var timeTableId = $('#tableID').data('timetableId');
console.log(timeTableId);
$("#processSUB").load("hello.php", { timetableID : timeTableId });
}

Categories