Refresh page if certein php script is executed - javascript

I have a somefile.php and someotherfile.js with the code as below
javascript file
function deleteSelectedRow() {
return (confirm('Are you sure you want to delete this record))
};
<!DOCTYPE html>
<html lang=" en">
<head>
<title> Title </title>
</head>
<body>
<h1>Select the user to delete from the list below </h1>
<form action="" method="POST">
<?php
if(require_once('../SQL/mySQL_connect.php'))
{
$query = "SELECT id, FirstName, LastName, PhoneNumber FROM participants ORDER BY id ASC";
$userDetails = #mysqli_query($mysqli, $query);
}
else
{
echo "Couldn't connect to database";
echo mysqli_error($mysqli);
}
// mysqli_close($mysqli);
?>
<br><br><br>
<table name="userDetailsTable" id="userDetailsTable" align="left" cellspacing="7" cellpadding="8">
<tr>
<td align="center"><b>S No</b></td>
<td align="center"><b>Id</b></td>
<td align="center"><b>Rank</b></td>
<td align="center"><b>First Name</b></td>
<td align="center"><b>Last Name</b></td>
</tr>
<?php
for($i = 1; $i <= mysqli_num_rows($userDetails); $i++)
// while($row=mysqli_fetch_array($userDetails))
{
$row=mysqli_fetch_array($userDetails);
echo '<tr>
<td align ="center" >'. $i .'</td>
<td align ="center" >' . $row['id'] . '</td>
<td align ="center">' . $row['Rank'] . '</td>
<td align ="center">' . $row['FirstName'] . '</td>
<td align ="center">' . $row['LastName'] . '</td>
<td align ="center"> <input type = submit name="delete" value="delete" onclick="return deleteSelectedRow();" ></input></td>';
echo '</tr>';
}
?>
</table>
</form>
<?php
if(isset($_POST['delete']))
{
require_once('../SQL/mySQL_connect.php');
$query="DELETE FROM `participants` WHERE `participants`.`id` = ".$_POST['IDNumber']."";
$response = #mysqli_query($mysqli, $query);
if($response)
{
echo "Deleted from Database Successfully";
}
else
{
echo "Couldn't Delete from database";
echo'<br>';
echo mysqli_error($mysqli);
}
mysqli_close($mysqli);
}
?>
</body>
What this code does is as follows
Connects to database and retrieves the user details
Creates a table and prints out the user details in it
user clicks on delete button in front of any record and it gets deleted after confirmation
A success message is displayed that the message is deleted
What I want to do is that after displaying the success message the above printed table should get updated automatically so that user is confirmed that the id no longer exists in the table
I tried the following solutions
reload page just before the success message is displayed so that user sees the success message as well as the updated table as well (since reload will re-connect to database and refetch the table)
I tried to use "location.reload(true)" command but i can't figure out where to place this line so that it gets executed just before the success message is displayed.
Any help is much appreciated

A few things:
you'll want the delete operation to be the first thing you do on the page (if it's a form submit) because otherwise you'll print the "pre-deleted" table.
you need to pass the ID through post in the form. It's easier if you just have a unique for for every row, and have a hidden ID input for each.
The confirm is better attached to the form submit event, because otherwise you'll miss other, non-click, input methods.
Your delete operation, as it was written in the question, is susceptible to an SQL Injection attack. You'll want to escape that POST value.
Something like the below should work
function deleteSelectedRow() {
return (confirm('Are you sure you want to delete this record))
};
<?php
$message = '';
$connected = false;
if(require_once('../SQL/mySQL_connect.php'))
{
$connected = true;
}
if($connected && isset($_POST['delete']))
{
$id_to_delete = mysqli_real_escape_string($mysqli, $_POST['IDNumber']);//escape value to prevent sql injection attack
$query="DELETE FROM `participants` WHERE `participants`.`id` = ".$id_to_delete."";
$response = #mysqli_query($mysqli, $query);
if($response)
{
$message = "Deleted from Database Successfully";
}
else
{
$message = "Couldn't Delete from database";
$message .='<br>';
$message .= mysqli_error($mysqli);
}
//mysqli_close($mysqli);
}else{
$message = "unable to connect to database";
}
?><!DOCTYPE html>
<html lang=" en">
<head>
<title> Title </title>
</head>
<body>
<h1>Select the user to delete from the list below </h1>
<?php
if($connected)
{
$query = "SELECT id, FirstName, LastName, PhoneNumber FROM participants ORDER BY id ASC";
$userDetails = #mysqli_query($mysqli, $query);
}
else
{
echo "Couldn't connect to database";
echo mysqli_error($mysqli);
}
?>
<br><br><br>
<?php if($message){ /* do we have a success/error message from the delete operation? */ ?>
<p><?php echo $message; ?></p>
<?php } ?>
<table name="userDetailsTable" id="userDetailsTable" align="left" cellspacing="7" cellpadding="8">
<tr>
<td align="center"><b>S No</b></td>
<td align="center"><b>Id</b></td>
<td align="center"><b>Rank</b></td>
<td align="center"><b>First Name</b></td>
<td align="center"><b>Last Name</b></td>
</tr>
<?php
for($i = 1; $i <= mysqli_num_rows($userDetails); $i++)
// while($row=mysqli_fetch_array($userDetails))
{
$row=mysqli_fetch_array($userDetails);
echo '<tr>
<td align ="center" >'. $i .'</td>
<td align ="center" >' . $row['id'] . '</td>
<td align ="center">' . $row['Rank'] . '</td>
<td align ="center">' . $row['FirstName'] . '</td>
<td align ="center">' . $row['LastName'] . '</td>
<td align ="center"> <form action="" method="POST" onsubmit="return deleteSelectedRow();"><input type="hidden" name="IDNumber" value="'.$row['id'].'" /><input type = submit name="delete" value="delete"></form></td>';
echo '</tr>';
}
?>
</table>
<?php if($connected){
mysqli_close($mysqli);
} ?>
</body>

You need to store the Success/Error message in a $_SESSION["flash"] instead of show by echo and after delete the user you must redirect to the same page.
On the top of the page, if isset the $_SESSION["flash"] you can show the message and remove it from the session. In code:
if(isset($_POST['delete']))
{
require_once('../SQL/mySQL_connect.php');
$query="DELETE FROM `participants` WHERE `participants`.`id` = ".$_POST['IDNumber']."";
$response = #mysqli_query($mysqli, $query);
if($response)
{
$_SESSION["flash"] = "Deleted from Database Successfully";
}
else
{
$_SESSION["flash"] = "Couldn't Delete from database";
//echo'<br>';
//echo mysqli_error($mysqli);
}
mysqli_close($mysqli);
header('Location: '.$_SERVER['PHP_SELF']);
}
and on the top of the page of before isset($_POST['delete']):
if(isset($_SESSION["flash"])){
echo $_SESSION["flash"];
unset($_SESSION["flash"]);
}
don't forget to start_session() on the top of the page.
I'll notice that your code have a SQL Injection Vulnerability. You shouldn't do MySQL queries without validate GET and POST input data.

Related

Populating dynamic form row from generated list PHP

I'm making something for a stocktake.
I have a form that generates with 4 fields. item_code, item_name, packing, quantity
<form action="goods.php" method="post">
<table>
<!-- Headers -->
<tr>
<td><b>Item Code</b></td>
<td><b>Description</b></td>
<td><b>Packing</b></td>
<td><b>Quantity</b></td>
</tr>
<?php
for ($i = 0; $i <= 50; $i++) {
?>
<tr>
<td>
<INPUT TYPE="TEXT" NAME="item_code[<?php echo $i; ?>]" SIZE="6" VALUE="
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $_POST["item_code"][($i)];
}
?>"></td>
<td><?php
if (!empty($_POST["item_code"][($i)])) {
$result = FetchData($_POST["item_code"][($i)]);
echo $result['category'];
}
?>
</td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['item_name'];
}
?></td>
<td>
<?php
if (!empty($_POST["item_code"][($i)])) {
echo $result['packing'];
}
?></td>
<td><INPUT TYPE="TEXT" NAME="quantity[<?php echo $i; ?>]" SIZE="5" VALUE="
<?php
if (!empty($_POST["quantity"][($i)])) {
echo $_POST["quantity"][($i)];
} else {
echo "";
}
?>"></td>
A js function for the button
<script>
function fillForm(value) {
document.getElementById('value').innerHTML = value;
}
</script>
and a list that is generated with 3 fields. item_code, item_name, packing
<?php
$dbh = dbh_get();
$sql = 'SELECT * FROM goods as goods(item_code, sort) order by human_sort(goods.item_code)';
$v = array();
$stmt = $dbh->prepare($sql);
$stmt->execute();
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
print '
<tr>
<td class="buttonL" id="<php ' . $r['item_code'] . ' ?>" onclick="fillForm()">' . $r['item_code'] . '</td>
<td>' . $r['item_name'] . '</td>
<td>' . $r['packing'] . '</td>
</tr>' . "\n";
}
dbh_free($dbh);
?>
}
I want to put a button on each list row and when it's clicked it populates the first three fields in the form, leaving quantity to be filled out. Then when another is clicked it populates the next form row etc,. It's working fine manually entering from the list, but the list is nearly 5000 items so it's a hassle to keep searching then scrolling up and entering the values.
I don't see how to do this with PHP so I assume I need a javascript function, which is where I'm lost. Let me know if you need more info.

PHP & MySQL & JS editable table won't update records

So I'm trying to use a table to update some records in my database but each time I click on update it won't work and it won't do anything. A part of the code below was found in an another topic but it was incomplete so I added some other things.
Js script
$(function(){
$("#loading").hide();
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('update.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
This is the table fetching the rows from the database, however everything works besides updating.
HTML & PHP
<form method="post" action="update.php">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table table-striped table-dark">
<tr bgcolor="#df4662" style="color:#FFFFFF;">
<td>ID</td>
<td>Nickname</td>
<td>Name</td>
<td>Rank</td>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td contenteditable="true" id="id:<?php echo $row["id"]; ?>"><?php echo $row["id"]; ?></td>
<td contenteditable="true" id="username:<?php echo $row["username"]; ?>"><?php echo $row["username"]; ?></td>
<td contenteditable="true" id="name:<?php echo $row["steamid"]; ?>"><?php echo $row["steamid"]; ?></td>
<td contenteditable="true" id="ranks:<?php echo $row["ranks"]; ?>"><?php echo $row["ranks"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
After a few errors I've been able to have a clean error_logs, but now I don't get any error even after pressing the update button.
update.php
<?php
include '../database.php'
?>
<?php
if(!empty($_POST))
{
foreach($_POST as $field_name => $val)
{
$field_id = strip_tags(trim($field_name));
$split_data = explode(':', $field_id);
$id = $split_data[1];
$field_name = $split_data[0];
if(!empty($id) && !empty($field_name) && !empty($val))
{
$affected_rows = mysqli_query($mysqli,"UPDATE users SET $field_name = '$val' WHERE id = $id");
echo $affected_rows;
echo "Updated";
} else {
echo "Invalid Request";
}
}
}
else {
echo "Invalid Requests";
}
?>
EDIT: Thanking Sam now the problem is just that the record won't update at all

Choosing specific table to submit

I am currently calling tables using php in order to show pending purchase requests. An admin will either approve or deny these requests based on the contents of each table. Each table will have a unique identifier and this is how they are divided. I am trying to determine how I can approve/deny each table individually, but I'm new to jQuery. I've used it before in a similar manner but can't seem to find a solution for what I am trying to do.
Right now, I'm just trying to set up an alert to make sure that the function is working properly.
PHP code showing table format (code is in a while loop):
echo "<form method='POST' onsubmit='moveTable(this)'>
<table id='pendingTable'>
<tbody>
<tr style='background-color:$bgcolor'>
<input type='hidden' value='".$RNrow['request_number']."'>
<td id='name'>".$PRrow['ItemName']."</td>
<td>".$PRrow['ItemDesc']."</td>
<td>".$PRrow['BrandName']."</td>
<td>".$PRrow['ManNum']."</td>
<td>".$PRrow['NSN']."</td>
<td>".$PRrow['ItemCost']."</td>
<td>".$PRrow['Qty']."</td>
</tr>
</tbody>";
<input type='submit' value='Approve' onclick=\"return confirm ('Are you sure you want to approve this request?')\">
<input style='margin-left:5px' type='submit' value='Deny' onclick=\"return confirm ('Are you sure you want to deny this request?')\">
<script type="text/javascript">
function moveTable(){
$('#pendingTable tr').each(function(){
alert('hello');
});
}
</script>
The information from these tables would then be moved to an 'approved/denied' mysql table. I figure it may have to do with the uniqueness of the table id but haven't found a way around that. Any help would be appreciated.
You cannot use id as it is supposed to be unique within the DOM, you can use classes instead like this:
echo "<form method='POST' onsubmit='moveTable(this)'>
<table class='pendingTable'>
<tbody>
<tr style='background-color:$bgcolor'>
<input type='hidden' value='".$RNrow['request_number']."'>
<td id='name'>".$PRrow['ItemName']."</td>
<td>".$PRrow['ItemDesc']."</td>
<td>".$PRrow['BrandName']."</td>
<td>".$PRrow['ManNum']."</td>
<td>".$PRrow['NSN']."</td>
<td>".$PRrow['ItemCost']."</td>
<td>".$PRrow['Qty']."</td>
</tr>
</tbody>";
<input type='submit' value='Approve' onclick=\"return confirm ('Are you sure you want to approve this request?')\">
<input style='margin-left:5px' type='submit' value='Deny' onclick=\"return confirm ('Are you sure you want to deny this request?')\">
<script type="text/javascript">
function moveTable(form){
$(form).find('.pendingTable tr').each(function(){
alert('hello');
});
}
</script>
You can paste this entire script into one PHP script and you should be able to see how it passes the table id via ajax to the script at the bottom of the page. Give it a try!
<?php if (!isset($_POST['doAjaxAction'])) { ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Dev Doc</title>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<style>
form {
margin-bottom: 50px;
}
</style>
</head>
<body>
<?php
$RNrow['request_number'] = '123';
$RNrow['ItemName'] = 'ItemName';
$RNrow['ItemDesc'] = 'ItemDesc';
$RNrow['BrandName'] = 'BrandName';
$RNrow['ManNum'] = 'ManNum';
$RNrow['NSN'] = 'NSN';
$RNrow['ItemCost'] = 'ItemCost';
$RNrow['Qty'] = 'Qty';
$bgcolor = 'grey'
?>
<form method='POST'>
<table id='<?php echo $RNrow['request_number'] ?>' class="pendingTable">
<tbody>
<tr style='background-color:<?php echo $bgcolor; ?>'>
<input type='hidden' value='<?php echo $RNrow['request_number'] ?>'>
<td id = 'name'>"<?php echo $RNrow['ItemName'] ?>"</td>
<td>"<?php echo $RNrow['ItemDesc'] ?>"</td>
<td>"<?php echo $RNrow['BrandName'] ?>"</td>
<td>"<?php echo $RNrow['ManNum'] ?>"</td>
<td>"<?php echo $RNrow['NSN'] ?>"</td>
<td>"<?php echo $RNrow['ItemCost'] ?>"</td>
<td>"<?php echo $RNrow['Qty'] ?>"</td>
</tr>
</tbody>
<input type='submit' value='Approve' onclick="moveTable(<?php echo $RNrow['request_number'] ?>, 'Approve')">
<input style='margin-left:5px' type='submit' value='Deny' onclick="moveTable(<?php echo $RNrow['request_number'] ?>, 'Deny')">
</table>
</form>
<?php
$RNrow['request_number'] = '456';
$RNrow['ItemName'] = 'ItemName2';
$RNrow['ItemDesc'] = 'ItemDesc2';
$RNrow['BrandName'] = 'BrandName2';
$RNrow['ManNum'] = 'ManNum2';
$RNrow['NSN'] = 'NSN2';
$RNrow['ItemCost'] = 'ItemCost2';
$RNrow['Qty'] = 'Qty2';
$bgcolor = 'lightblue'
?>
<form method='POST'>
<table id='<?php echo $RNrow['request_number'] ?>' class="pendingTable">
<tbody>
<tr style='background-color:<?php echo $bgcolor; ?>'>
<input type='hidden' value='<?php echo $RNrow['request_number'] ?>'>
<td id = 'name'>"<?php echo $RNrow['ItemName'] ?>"</td>
<td>"<?php echo $RNrow['ItemDesc'] ?>"</td>
<td>"<?php echo $RNrow['BrandName'] ?>"</td>
<td>"<?php echo $RNrow['ManNum'] ?>"</td>
<td>"<?php echo $RNrow['NSN'] ?>"</td>
<td>"<?php echo $RNrow['ItemCost'] ?>"</td>
<td>"<?php echo $RNrow['Qty'] ?>"</td>
</tr>
</tbody>
<input type='submit' value='Approve' onclick="moveTable(<?php echo $RNrow['request_number'] ?>, 'Approve')">
<input style='margin-left:5px' type='submit' value='Deny' onclick="moveTable(<?php echo $RNrow['request_number'] ?>, 'Deny')">
</table>
</form>
<script>
function moveTable(id, action) {
//Opens OK or Cancel Dialog
if (confirm('Are you sure you want to approve this request?')) {
//Hit F12 in your browser to the console. This shows what passed in
alert('ID Sent: ' + id, ' -- Action: ' + action);
//Posts ID and Action to self, with 'doAjaxAction' set to 1, so only the php script at bottom executes during AJAX call
$.post('<?php echo basename($_SERVER['PHP_SELF']); ?>',
{
//seen as $_POST['id'] and $_POST['action'] to script at the bottom
doAjaxAction: '1',
id: id,
action: action
},
//This function gives us the data returned from approve_deny.php
function (data, status) {
// 'data' returns 'DID SOMETHING'
// 'status' returns status of ajax call
alert("Data Sent: " + data + "\nStatus: " + status);
if (status === 'success') {
//Removes the order from the page. Can change this to grey it out, shrink it, etc
alert('AJAX call was a.. ' + status);
// $("#" + id + "\"").empty();
}
});
}
//If user changes mind, and clicks cancel on confirm
else {
alert('Action Cancelled.')
}
}
</script>
</body>
</html>
<?php } ?>
<?php
if (isset($_POST['doAjaxAction']) && $_POST['doAjaxAction'] == '1') {
$id = $_POST['id'];
$action = $_POST['action'];
if ($action == 'Approve') {
echo "APPROVED ID # " . $_POST['id'] . " do something with passed ID " . PHP_EOL;
}
if ($action == 'Deny') {
echo "DENIED ID # " . $_POST['id'] . " do something with passed ID " . PHP_EOL;
}
echo "DID SOMETHING AT END" . PHP_EOL;
}
?>

Disable Submit button once database field is filled

I have created a form with submit button. If "price" for a particular product is already filled then the Submit button must be disabled:
Code Php
<?php
$host="localhost";
$username="root";
$password="";
$db_name="ge";
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("Your Connection is in error");
$sql="SELECT pname,catogery,email FROM quetation WHERE catogery = '$catogery'";
$results=mysqli_query($con,$sql);
$count=mysqli_num_rows($results);
if($count == 0) {
echo "<font color=\"#0000\"><h1 align=\"center\">No details found</h1></font>";
} else {
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>ProdName</b></td>
<td><b>Catogery</b></td>
<td><b>Price</b></td>
</tr> ";
while($result=mysqli_fetch_array($resource)) {
echo "
<div class=\"row\">
<div class=\"input-field col s12\">
<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td>
<form name=\"abc\" methos=\"post\" action=\"postprice.php\">
<input type=\"submit\" value=\"send\">
</form>
</td>
</div>
</div>
</tr>";
} echo "</table></font>";
}
?>
Only once price should be entered if already entered then send button should be disabled
Whenever I load this page it should check the database and if price value is filled then it should disable the send button.
Use the below code in the submit button:
<input type="submit" onClick="this.disabled=true;">
This will disable the button after clicking the submit button. Hope it helps you.
PLz try this code, hope it will work.
<?php
$host="localhost";
$username="root";
$password="";
$db_name="ge";
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("Your Connection is in error");
$sql="SELECT pname,catogery,email,price FROM quetation WHERE catogery = '$catogery'";
$results=mysqli_query($con,$sql);
$count=mysqli_num_rows($results);
if($count == 0)
{
echo "<font color=\"#0000\"><h1 align=\"center\">No details found</h1></font>";
}
else
{
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>ProdName</b></td>
<td><b>Catogery</b></td>
<td><b>Price</b></td>
</tr> ";
while($result=mysqli_fetch_array($resource))
{
echo "
<div class=\"row\">
<div class=\"input-field col s12\">
<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td><form name=\"abc\" methos=\"post\" action=\"postprice.php\">";
if($result[3]==''){
echo "<input type=\"submit\" value=\"send\" onClick='this.disabled=true;'>";
} else {
echo "<input type=\"submit\" value=\"send\" disabled>";
}
echo "</td>
</form>
</div>
</div>
</tr>";
}echo "</table></font>";
}
?>
first you will check in database product price is available or not if price is available then set the variable 0 for not available or 1 for available;

Hyperlink with variables to create a modal box/popup

Like in the title I would like to use a variable inside the hyperlink to use it into the modal window.
I am not using bootstrap, it is a custom code but it works until I try to put some kind of <a href='#openModal?id=".$VARIABLE."'>
Is it possible to do that?
Regards
Update:
<?php
$query = "SELECT * FROM USER";
$result = mysqli_query ($connection,$query)
or die ("You couldn’t execute query");
echo "<div class='admin-table'>
<table cellspacing='15'>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC))
{
extract ($row);
echo "<tr>\n
<td>$USER_LASTNAME</td>\n
<td>$USER_FIRTSNAME</td>\n
<td>$USER_PHONE</td>\n
<td>$USER_ADDRESS</td>\n
<td>$USER_POSTCODE</td>\n
<td>$USER_DOB</td>\n
<td>$USER_EMAIL</td>\n
<td>$USER_PASSWORD</td>\n
<td>$USER_ROLE</td>\n
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>\n
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>\n
</tr>\n";
echo "<tr><td colspan ='15'><hr></td></tr>\n";
}
echo "</table></div>\n";
?>
<div id="openModal?id=<?php echo $USER_ID; ?>" class="modalDialog">
<div>X
<h2>$USER_ID</h2>
</div>
</div>
It is working the modal but its just taking the last id, I have to think another solution to pass the variable.
Many thanks for your help
Update 2:
Thank you very much! Now its working,
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal?id=".$USER_ID."'>Edit</a></td>
<div id='openModal?id=".$USER_ID."' class='modalDialog'>
<div><a href='#close' title='Close' class='close'>X</a>
<h2>".$USER_ID."</h2>
<p>You can have additional details here.</p>
</div>
</div>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
?>
Try this:
echo '<a href="#openModal?id='.$VARIABLE.'">';
Update:
echo '<td>Edit</td>\n';
echo '<td>Delete</td>\n';
any reason you can't use onclick ?
<a href="#" onclick="myJsFunc();">
<script>
function myJsFunc() {
//code to open modal
}
</script>
UPDATE
If i have understood you correct you are trying to make a table showing all users and then when the admin clicks at element a modal pop ups providing additional info for the particular user.
<?php
$query = "SELECT * FROM USER;";
$result = mysqli_query ($connection,$query) or die ("You couldn’t execute query");
//First echo the table with all your data as you want
echo "
<div class='admin-table'>
<table cellspacing='15'>
<tr>
<td><h3>Last Name</h3></td>
<td><h3>Name</h3></td>
<td><h3>Phone</h3></td>
<td><h3>Address</h3></td>
<td><h3>Postcode</h3></td>
<td><h3>Date of Birth</h3></td>
<td><h3>Email</h3></td>
<td><h3>Password</h3></td>
<td><h3>Role</h3></td>
</tr>";
//Fetch all rows for each user
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo "
<tr>
<td>$USER_LASTNAME</td>
<td>$USER_FIRTSNAME</td>
<td>$USER_PHONE</td>
<td>$USER_ADDRESS</td>
<td>$USER_POSTCODE</td>
<td>$USER_DOB</td>
<td>$USER_EMAIL</td>
<td>$USER_PASSWORD</td>
<td>$USER_ROLE</td>
<td><a href='admin_user.php?id=".$USER_ID."'>Delete</a></td>
<td><a href='#openModal".$USER_ID."'>Edit</a></td>
</tr>
<tr>
<td colspan ='15'><hr></td>
</tr>";
}
echo"
</table>
</div>";
//Fetch again the rows to make the modals with the edit info
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
extract ($row);
echo '
<div id="openModal'.$USER_ID.'" class="modalDialog">
<div>X
<h2>'.$USER_ID.'</h2>
<p>You can have additional details here.</p>
</div>
</div>';
}
?>
Old answer before the comments
It seems that single quotes and/or double quotes aren't escaped properly.
Also remember that in PHP string concatenation is made using " . " (dot) and in JavaScript using " + " (plus).
Update 1
I think that if you use the following you will be ok.
echo '<td>Delete</td>';
echo '<td>Edit</td>';
Update 2
Don't forget to add $variable at div too so <a> and div id are the same the same. E.g.
echo '<div id="openModal'.$variable.'" class="modalDialog"></div>';

Categories

Resources