How to Make delete confirmation modal using PHP - php

When I click the delete button I want to make a confirmation on a Bootstrap modal, before the user can delete it. The problem is that i don't know how to passed the $muestra['id'] from the data table to my modal.
<?php
$busqueda=$conn->query("select * from productos");
$arrDatos=$busqueda->fetchAll(PDO::FETCH_ASSOC);
foreach ($arrDatos as $muestra) {
echo "<tr>
<td>" . $muestra['id'] . "</td>
<td>" . $muestra['nombre'] . "</td>
<td>" . $muestra['unidades'] . "</td>
<td>
<a href='del.php?id=".$muestra['id']."' class='btn btn-danger' data-bs-toggle='modal' data-bs-target='#deleteModal'>
<ion-icon name='trash-outline'></ion-icon>
</a>
</td>
</tr>
<!-- Modal -->
<div class='modal fade' id='deleteModal' tabindex='-1' aria-labelledby='deleteModalLabel' aria-hidden='true'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<h1 class='modal-title fs-5' id='deleteModalLabel'>¿Estás seguro de que quieres borrar el elemento?</h1>
<button type='button' class='btn-close' data-bs-dismiss='modal' aria-label='Close'></button>
</div>
<div class='modal-body'>
¿Estás seguro de que quieres borrar el elemento?
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-secondary' data-bs-dismiss='modal'>No</button>
<a href='del.php?id=".$muestra['id']."' class='btn btn-danger'>Sí</a>
</div>
</div>
</div>
</div>
<!-- End Modal -->
";
}
?>
</table>
I've tried many things, I know it's a simple problem, I've searched in old answers, but I'm new to coding and I can't solve the problem.

There will be need of some JS since that is done on the browser side. Using JS you get the id attribute of the modal when the button is click and do your logic.

Related

trying to send get value to modal

I'm trying to send the get variable from a anchor href to modal. but I cant seem to get the variable $_GET['edit2']. when I try to echo the variable it echo null which is obtain from the else statement. Thank you in advance
Part 1 : used to check if the get variable have been submitted or not.
if(isset($_GET['edit2'])){
$_SESSION['reply_id'] = $_GET['edit2'];
$reply_id = $_SESSION['reply_id'];
}
else{
$reply_id = "null";
}
Part 2 : the anchor href
<a href='?edit2=". $row2['reply_id'] ."' data-toggle='modal' data-target='#myModal'><small>edit</small></a>
part 3 : the modal form
<div class='modal fade' id='myModal' role='dialog'>
<div class='modal-dialog'>
<!-- Modal content-->
<div class='modal-content row'>
<div class='col-12'>
<div class='modal-header'>
<h5 class='modal-title'>Edit Reply</h5>
<button type='button' class='close' data-dismiss='modal'>×</button>
</div>
<div class='modal-body'>
<form id='r_edit_form' method='post'>
<div class='row'>
<label class='col-12'>Reply :</label>
</div>
<div class='row'>
<textarea class='col-12' form='r_edit_form' name='description' rows='4'>". $reply_id ."</textarea>
</div>
<div class='row'>
<input class='col-12 btn btn-dark' type='submit' name='edit_reply'>
</div>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
</div>

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!!

"If" Statement and Session

I have a form that is posting sessions back to my index page. Based on those sessions I am using echo to load a modal window from the results. I keep getting a 500 error. I can't figure out why.
if($_SESSION["submitemail"] == "fail"){
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<div class='modal fade' role='dialog' id='formerror'>
<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'>Could not send message...</h4>
</div>
<div class='modal-body'>
<?
echo '<p>We are very sorry, but there were error(s) found with the form you submitted.</p>';
echo '<p>These errors appear below.</p><br /><br />';
echo $_SESSION['errormessage'].'<br /><br />';
echo '<p>Please go back and fix these errors.</p><br /><br />';
?>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
else if($_SESSION["submitemail"] == "success"){
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<!-- Modal -->
<div class='modal fade' role='dialog' id='formsent'>
<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'>Thank You <? echo $_POST['first_name']; ?> <? echo $_POST['last_name']; ?>!</h4>
</div>
<div class='modal-body'>
<?
echo '<p>We appreciate your business and will be contacting you as soon as possible. Please allow 48 hours for us to process your request before sending another. Thank you!/p>';
?>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
else {
echo
"<script type='text/javascript'>
$(window).load(function(){
$('#betaalert').modal('show');
});
</script>
<!-- Modal -->
<div class='modal fade' role='dialog' id='betaalert'>
<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'>ANNOUNCEMENT!</h4>
</div>
<div class='modal-body'>
<p>However we are pleased to announce that our beta testing program is open to all ambulance companies interested. If you are interested use the contact form on the bottom of this page to send us a message with your contact information and we will get you started with testing ASAP!</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn btn-primary' data-dismiss='modal'>Close</a>
</div>
</div>
</div>
</div>";
}
Nevermind, I figured it out! Thank you though, and for the future, I will make sure to post more information.

Twitter Bootstrap Modal Dialog not working well.

Good day, I am displaying members of a team each on a DIV. For each DIV, there is a message button for a user to send message. The message button fires up a Bootstrap modal dialog box where one can type the message. Now, my problem is that I want the name of person the message is being sent to, to appear at the top of the dialog box but it is not working. It only shows the name of the first person in the database even when I click on the third person.
<?php
require_once "include/db_handle.php";
$sql = "SELECT i.*, m.* FROM addclique i JOIN members m ON m.id = i.clique_id WHERE adder_id = :id";
foreach ($db->query($sql, array('id' => $_SESSION['id'])) AS $result)
{
echo "
<div class='user_container'>
Name: </span> {$result['surname']} {$result['firstname']}</br>
<a href='test.php' class='' data-toggle='modal' data-target='#basicModal'>Send Message</a>
</div>
<div class='modal fade' id='basicModal' tabindex='-1' role='dialog' aria-labelledby='basicModal' 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 points' id='myModalLabel'><span style ='color:black;'>To:</span> {$result['firstname']} </h4>
</div>
<div class='modal-body'>
<form action='send.php' method='POST'>
<textarea name='message' rows='10' cols='65'></textarea></br>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
<button type='button' class='btn btn-primary' data-dismiss='modal'>Send Message</button>
</div>
</div>
</div>
</div>
</div>";
}
?>
<a href='test.php' class='' data-toggle='modal' data-target='#**basicModal**'>Send Message</a>
<div class='modal fade' id='**basicModal**' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
If you really want to do a modal for each user, consider do a variable for your modals.
Not sure, but something like that.
var $counter = 0;
foreach ($db->query($sql, array('id' => $_SESSION['id'])) AS $result)
{
echo "
<div class='user_container'>
Name: </span> {$result['surname']} {$result['firstname']}</br>
<a href='test.php' class='' data-toggle='modal' data-target='#basicModal".<?= $counter ?>." '>Send Message</a>
</div>
<div class='modal fade' id='basicModal".<?= $counter ?>."' tabindex='-1' role='dialog' aria-labelledby='basicModal' 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 points' id='myModalLabel'><span style ='color:black;'>To:</span> {$result['firstname']} </h4>
</div>
<div class='modal-body'>
<form action='send.php' method='POST'>
<textarea name='message' rows='10' cols='65'></textarea></br>
</form>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
<button type='button' class='btn btn-primary' data-dismiss='modal'>Send Message</button>
</div>
</div>
</div>
</div>
</div>";
$counter++;
}
?>
You are creating modal for each user with same id i.e. basicModal. So if total user is 5 then 5 modal with same id is created on html page.
<a data-toggle='modal' data-target="#basicModal">Show Modal</a>
And in the above link you are calling #basicModal.So the js cant find the that particular user modal id because it is not unique.So it display the first modal with that id(or display none)
The problem should be solved if you use unique id in the link and modal .But a better option would be creating a single modal and setting the data through js so that multiple modal html is not placed in page.If you want to do it with jquery
Send Message
In jquery
$("#showModal").on("click",function(){
//set modal data
});

I am having problems displaying a result from an array in a while loop

I am new to php and am having a problem echoing a result from the database. Everything above the div tags executes fine and displays in my table properly. But the problem I am having is within the div tags.
while($row = mysql_fetch_array($result))
{
if ($row['status']==0){
$row['status']="Inactive";
$lablestatus="label";
}
elseif ($row['status']==1){
$row['status']="Pending";
$lablestatus="label label-warning";
}
elseif ($row['status']==2){
$row['status']="Banned";
$lablestatus="label label-important";
}
elseif ($row['status']==3){
$row['status']="Active";
$lablestatus="label label-success";
}
echo "<tr>
<td>{$row['id']}</td>
<td class='center'>{$row['username']}</td>
<td class='center'>17</td>
<td class='center'>36</td>
<td class='center'>17</td>
<td class='center'>$458.66</td>
<td class='center'>Yes</td>
<td class='center'>{$row['register_date']}</td>
<td class='center'>2013-02-13 24:06:13</td>
<td class='center'>Yes</td>
<td class='center'><span class='$lablestatus'>{$row['status']}</span></td>
<td class='center'>
<a class='btn btn-success' href='view_user.php?id={$row['id']}'>
<i class='icon-zoom-in icon-white'></i>View</a>
<a class='btn btn-info' href='edit_user.php?id={$row['id']}'>
<i class='icon-edit icon-white'></i>Edit</a>
<a class='btn btn-danger btn-setting'>
<i class='icon-trash icon-white'></i>Delete</a>
</td>
</tr>
Here is where the problem occurs. When I click delete for a certain user, a popup is displayed. It asks "Are you sure you want to delete the user exampleuser?" The same username is displayed for each user in my table. So if i click delete for exampleuser2, It will ask if I want to delete exampleuser. Any idea how to fix this?
<div class='modal hide fade' id='myModal'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h3>Delete member</h3>
</div>
<div class='modal-body'>
<p>Are you sure you want to delete the user {$row['username']}?</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn' data-dismiss='modal'>No</a>
<a href='delete_user.php?id={$row['id']}' class='btn btn-primary'>Yes</a>
</div>
</div>";
}
AS your pop up code is inisde while loop
<div class='modal hide fade' id='myModal'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>×</button>
<h3>Delete member</h3>
</div>
<div class='modal-body'>
<p>Are you sure you want to delete the user {$row['username']}?</p>
</div>
<div class='modal-footer'>
<a href='#' class='btn' data-dismiss='modal'>No</a>
<a href='delete_user.php?id={$row['id']}' class='btn btn-primary'>Yes</a>
</div>
</div>";
Your div id is not unique id='myModal', I guess all the time it takes for the first/last div. There can not be multiple id a page
Make it dynamic <div class='modal hide fade' id='myModal_{$row['id']}'> some thing like this and change your js pop up code and try (I guess your are specifying this id while clicking the delete link there also make it dynamic to match proper pop ups).

Categories