I have a table of chapters for which I have a delete button for each one. When I am clicking on delete button if the table has more records further the last row of the table gets deleted or the id of row getting pass to the database is wrong.
<!doctype html>
<html>
<head>
<title>Files</title>
</head>
<body>
<form method="post" action="deleteFiles.php" enctype="multipart/form-data">
<style>
td {
text-align: center;
border: 1px solid black;
border-collapse: collapse;
}
</style>
Select rank :
<select name="type" id="type" onchange="this.form.submit()">
<?php if(isset($_POST['type']))
{ ?>
<option value="1" <?php echo($_POST['type']==1?"selected":"");?>>SSgt</option>
<option value="2" <?php echo($_POST['type']==2?"selected":"");?>>TSgt</option>
<option value="3" <?php echo($_POST['type']==3?"selected":"");?>>MSgt</option>
</select>
<br><br>
<?php
}
else
{
?>
<option value="1">SSgt</option>
<option value="2">TSgt</option>
<option value="3">MSgt</option>
</select>
<br><br>
<?php
}
?>
<table id="example" style="width:60%">
<tr>
<th><font size="5">Files</font></th>
</tr>
<?php
?>
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=174;dbname=airman', 'airman', 'airma');
if(isset($_POST['action'])) {
$stmt = $dbh->prepare("DELETE FROM `files` WHERE `id`= " . $_POST['id']);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($result) > 0)
{
echo 'row deleted';
}
else{
echo 'row could not delete';
}
}
if(isset($_POST['type']))
{
$stmt = $dbh->prepare("SELECT * FROM files where type = :type");
$stmt->bindParam("type", $_POST['type']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
}
else{
$stmt = $dbh->prepare("SELECT * FROM files");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
}
foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td><input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />
</tr>
<?php
}
?>
</table>
</body>
</form>
</html>
<?php
?>
EDIT:
<!doctype html>
<html>
<head>
<title>Video Files</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('#form').submit(function(ev) {
$('<input type="hidden">').attr({ name: 'type', value: $("select[name='type']").val() }).appendTo($(event.currentTarget));
}
</script>
<form id="form" method="post" action="deleteVideoFiles.php" enctype="multipart/form-data">
<style>
td {
text-align: center;
border: 1px solid black;
border-collapse: collapse;
}
</style>
<br><br><br><br>
Select rank :
<select name="type" id="type" onchange="this.form.submit()">
<?php if(isset($_POST['type']))
{
/* session_start();
$_SESSION['vidType'] = $_POST['type'];*/
?>
<option value="">Select rank...</option>
<option value="1" <?php echo($_POST['type']==1?"selected":"");?>>SSgt</option>
<option value="2" <?php echo($_POST['type']==2?"selected":"");?>>TSgt</option>
<option value="3" <?php echo($_POST['type']==3?"selected":"");?>>MSgt</option>
</select>
<br><br>
<?php
}
else
{
/* session_start();
$_SESSION['vidType'] = $_POST['type'];*/
?>
<option value="">Select rank...</option>
<option value="1">SSgt</option>
<option value="2">TSgt</option>
<option value="3">MSgt</option>
</select>
<br><br>
<?php
}
?>
<table id="example" style="width:80%">
<tr>
<th><font size="5">Video Files</font></th>
</tr>
<?php
?>
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=174.75.54;dbname=handbook', 'airman', 'airman12345');
// $dbh = new PDO('mysql:host=localhost;dbname=handbook', 'siddhi', 'siddhi');
if(isset($_POST['action'])) {
$stmt = $dbh->prepare("DELETE FROM `videos` WHERE `id`= :id");
$stmt->bindParam("id", $_POST['id']);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($result) < 0)
{
echo 'row could not delete';
}
}
if(isset($_POST['type']))
{
if(!empty($_POST['type']))
{
$stmt = $dbh->prepare("SELECT * FROM `videos` where type = :type");
$stmt->bindParam("type", $_POST['type']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}
else{
echo 'empty type';
$stmt = $dbh->prepare("SELECT * FROM `videos`");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}
}
/* elseif(!empty($_SESSION['vidType'])){
echo $_SESSION['vidType'];
echo $_POST['type'];
$stmt = $dbh->prepare("SELECT * FROM `videos` where type = :type");
$stmt->bindParam("type", $_SESSION['vidType']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}*/
else{
$stmt = $dbh->prepare("SELECT * FROM `videos`");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}
foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td>
<form method="post" action="deleteVideoFiles.php" enctype="multipart/form-data">
<input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete"><br>
<input type="hidden" name="id" value="<?php echo $file['id']?>"/>
</form> <!-- notice how we built different form for every id-->
</td>
</tr>
<?php
}
?>
</table>
</body>
</form>
</html>
<?php
?>
I am sending the id of chapter in for each loop, What is the problem here? Please help thank you..
That is because building the form element this way is going to result for many id inputs inside the same form. something like this
<form>
<input name="id" value="1">
<input name="id" value="2">
</form>
on your server code the php will set $_POST['id'] to the last one which in this case 2. that is why when press delete button and submit the form you delete the next , or last id you have built in your form
If you need your code work the way you want you have to put every id in different form element, Or you will need some javascript work.
I prefer building different form for each record. replace that code
foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td><input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />
</tr>
<?php
}
?>
with something like this
foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td>
<form method="post" action="deleteFiles.php" enctype="multipart/form-data">
<input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />
</form> <!-- notice how we built different form for every id-->
</td>
</tr>
<?php
}
?>
Another note :
$stmt = $dbh->prepare("DELETE FROM `files` WHERE `id`= " . $_POST['id']);
using prepared statements this way makes you still vulnerable to SQL injection.
EDIT(reaction to comments)
to send another input along with the delete forms to the PHP server. you can use JavaScript for that.
1- add that class deleteForm to the delete forms so we can catch those forms and bind to only them.
<form method="post" class="deleteForm" action="deleteVideoFiles.php" enctype="multipart/form-data">
2- edit that script tag in your document
<script>
$(function (){
$('form.deleteForm').submit(function(ev) {
$('<input type="hidden">').attr({ name: 'type', value: $("select[name='type']").val() }).appendTo($(ev.currentTarget));
});
})
</script>
Related
After php and mysql outputs a list of data in a table with radio buttons, dropdown boxes and text boxes for each row from the database, I'd like to be able to AJAX update the database onclick of radio button, dropdown boxes or entry of text. This is what I have...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<?php
require_once 'config.php';
echo '<table style="margin:0 auto;">
<tr>
</tr>';
$sql = "SELECT id, address, suburb, lat, lng, day, date, time FROM addresses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td><span style="font-weight:bold;">'. $row["address"].' '. $row["suburb"].'</span> <button style="float:right;" type="button">Go</button><br><br>
H<input type="radio" name="home['. $row["id"].']" value="1"/>
NH<input type="radio" name="home['. $row["id"].']" value="2"/>
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time1" size="2">
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time2" size="2"><br>
<input style="width:100%; margin-top:5px;" type="text" name="notes" placeholder="Add note">
<br><br>
</td>
<td>
</td>
</tr>';
}
} else {
echo "0 results";
}
echo '</table>';
$conn->close();
?>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var home = $(this).val();
$.ajax({
url:"updateaddress.php",
method:"POST",
data:{home:home},
});
});
});
</script>
<?php
// Include config file
require_once 'config.php';
$id = mysqli_real_escape_string($conn, $_POST['id']);
$home = mysqli_real_escape_string($conn, $_POST['home']);
if(isset($_POST["home"])) {
$sql = "UPDATE addresses SET home='$home' WHERE id=$id";
if($conn->query($sql) === TRUE){
} else {
echo "error" . $sql . "<br>".$conn->error;
}
}
mysqli_close($conn);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
?>
I can't figure out what I need to do to get this to work.
Please help.
You need to pass the id of the row in to that ajax page...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<?php
require_once 'config.php';
echo '<table style="margin:0 auto;">
<tr>
</tr>';
$sql = "SELECT id, address, suburb, lat, lng, day, date, time FROM addresses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td><span style="font-weight:bold;">'. $row["address"].' '. $row["suburb"].'</span> <button style="float:right;" type="button">Go</button><br><br>
H<input type="radio" name="home['. $row["id"].']" id="'. $row["id"].'" value="1"/>
NH<input type="radio" name="home['. $row["id"].']" id="'. $row["id"].'" value="2"/>
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time1" size="2">
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time2" size="2"><br>
<input style="width:100%; margin-top:5px;" type="text" name="notes" placeholder="Add note">
<br><br>
</td>
<td>
</td>
</tr>';
}
} else {
echo "0 results";
}
echo '</table>';
$conn->close();
?>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var home = $(this).val();
var id = $(this).attr('id');
$.ajax({
url:"updateaddress.php",
method:"POST",
data:{home:home,id:id},
});
});
});
</script>
<?php
// Include config file
require_once 'config.php';
$id = mysqli_real_escape_string($conn, $_POST['id']);
$home = mysqli_real_escape_string($conn, $_POST['home']);
if(isset($_POST["home"])) {
$sql = "UPDATE addresses SET home='$home' WHERE id=$id";
if($conn->query($sql) === TRUE){
} else {
echo "error" . $sql . "<br>".$conn->error;
}
}
mysqli_close($conn);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
?>
code:
<script>
$(document).ready(function(){
$(".menu").click(function(){
ids = $('.menu:checked').map(function() {
return this.id;
}).get().join(',');
console.log(ids);
$("#ids").val(ids);
});
});
</script>
<?php
if(isset($_POST['submit']))
{
$adminid = $_POST['admin'];
$menuids = explode(",", $_POST['ids']);
foreach ($menuids as $idd)
{
$sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
$result = mysqli_query($link,$sql);
}
if($result == true)
{
$msg .= "<p style='color:green'>successfull</p>";
}
else
{
$msg .= "<p style='color:red'>error!</p>";
}
}
?>
<form method="post">
<select name="admin" id="admin">
<option value="">---Select Admin---</option>
<?php
$sql = "select * from admin";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option>
<?php
}
?>
</select>
<table>
<tr>
<th>Share</th>
<th>Menu Name</th>
</tr>
<?php
$query = "select * from menu";
$results = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
</table>
<input type="text" name="ids" id="ids" value=""/>
<input type="submit" name="submit" id="submit" />
</form>
In this code I am update a table having name menu in database. Now, I want to check only those checkbox where admin_id like ,1, or ,2, which is update by query. How can I fix this issue ?please please help.
Thank You
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or
$fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
I am trying to populate Material Description from the Material Number. Bot hte values are stored in same SQL tasble. So what I want when I select Material Maiterial Description shpul auto populate.
Fileds in table are Material & MaterialDescripotion
Below is code in in main file where data is fetched
<?php
include_once "dbConnect.php";
$sql = "SELECT * FROM DRLINK";
$result2 = mysqli_query($conn, $sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<html>
<!DOCTYPE html>
<head>
<title>Dropdown Ajax</title>
</head>
<body>
<form action ="DSSTRsubmit.php" method="POST">
<table border="1">
<tr>
<td>Select Retailer</td>
</tr>
<tr>
<td>
<?php
echo "<select>";
echo $options;
echo "</select>";
?>
</td>
</tr>
</table>
<br/>
<br/>
<br/>
<table border="1">
<tr>
<td>Material</td>
<td>Material Description</td>
<td>Quantity</td>
<td>Unit of Measure</td>
</tr>
<tr>
<td>
<div class="Material">
<select name="Material" onchange="getId(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM MATERIALLIST";
$results = mysqli_query($conn, $query);
foreach($results as $MATERIALLIST) {
?>
<option value="<?php echo $MATERIALLIST["Material"];?>"><?php echo $MATERIALLIST["Material"];?></option>
<?php
}
?>
</select>
</div>
</td>
<td>
<div class="MaterialDescription">
<select name="MaterialDescription" id="DesList">
<option value=""></option>
</select>
</div>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
function getId(val){
$.ajax({
type:"POST",
url:"getdata1.php",
data:"Material="+val,
success: function(data){
$("#DesList").html(data);
}
});
}
</script>
</td>
<td><input type="text" name="dquantity_name" /> </td>
<td><input type="text" name="duom_name" /> </td>
</tr>
</table>
<legend> </legend>
<p> <button type="submit" class="pure-button pure-button-primary">Send Stock</button>
<br>
<br>
<?php
echo "Distributor Page";
?>
</body>
</html>
below is the getdata1.php
<?php
include_once "dbConnect.php";
if(!empty($_POST["Material"])){
$Material= $_POST["Material"];
$query = "SELECT * FROM MATERIALLIST WHERE Material = $Material";
$results = mysqli_query($conn,$query);
foreach($results as $MaterialDescription){
?>
<option value="<?php echo $Des["Material"];?>"><?php echo $materialDescription ["MaterialDescription"];?></option>
<?php
}
}
?>
Iam able to select the material but on selection of material no material description auto populates.
Thanks for the help
PHP is case sensitive. Be careful about naming variables.
foreach contains upper case variable, while echo - lowercase. Also in echo you have space after variable and before opening bracket.
$MaterialDescription
$materialDescription_["MaterialDescription"]
I have a list proxy.Once I select and save it I want to display the selected data. How can I do this?.I want to display it in selected list and once I reload or get back to that page `
<?php
include_once("../noaccess.php");
include_once(CLASS_PATH."fetch_service.php");
$objfetch = new fetchService();
include_once(CLASS_PATH."proxy.php");
$objproxy = new Proxy();
include_once(CLASS_PATH."log.php");
$objlog= new changelog();
$account_id=$_SESSION['account_id'];
global $mysqli;
if($_POST['submit']=="Save")
{
$pname=$_POST['proxy'];
$query="UPDATE `proxy` SET `proxy_default`='1' where `proxyname`='$pname'";
$mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
setcookie("msg","Proxy Seleted",time()+5,"/");
header("location:".SITE_URL."index.php?view=default_proxy");
}
?>
<form name="frmcr" id="frmcr" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" id="mode" value="insert" />
<table align="left" id="tblworking_hours" class="tbl_altcolor shadow" style="width:25%;margin-left:30px">
<thead>
<tr>
<th align="center"><b>Proxy</b></th>
<td><select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<!-- <option value="<?php echo $i ;?>"<?php echo $i==$ring21 ? "selected":"";?> ><?php echo $i;?></option> -->
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']== $resrproxy['proxyname'] ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Save" class="btn" style="margin-left:35px;"/></td>
</tr>
</thead>
</table>
</div>
You've to compare $resrproxy['proxyname'] value with $pname. Your code should be like this:
// your code
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy){
$option = "<option value=\"{$resrproxy['proxyname']}\"";
if(isset($pname)){
if($pname == $resrproxy['proxyname']){
$option .= " selected=\"selected\"";
}
}
$option .= ">{$resrproxy['proxyname']}</option>";
echo $option;
}
?>
</select>
// your code `
<td>
<select name="proxy" id="proxy" class="required input">
<option value="">Select</option>
<?php
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' and `proxy_default`='1'");
$pname=$result[0]['proxyname'];
$result = $objfetch->fetch_proxy("*","where account_id='".$_SESSION['account_id']."' ");
foreach($result as $key=>$resrproxy)
{
?>
<option value="<?php echo $resrproxy['proxyname'];?>"<?php echo $resrproxy['proxyname']==$pname ? "selected":"";?>><?php echo $resrproxy['proxyname'];?></option>
<?php } ?>
</select>
</td>
So I am having an issue with a drop down selection box. What I am doing is having someone log into a database, and then the database shows all the tables available in the selection box. The user can then select the table they wish to see, hit select and bam! There's the table information.
I am, however having an issue getting the data in the selection box to persist after they hit select. For some reason, it just makes it empty. I'm using session variables, and maybe that effects it? I'm just now beginning to learn how that works too. Take a look at let me know what you think:
<?php
session_start();
if(!isset($_SESSION['session_level'])):
$_SESSION['session_level'] = 0; ?>
<? endif ?>
<?php
if(isset($_POST['host'])):
$_SESSION['host'] = $_POST['host'];
$_SESSION['dbname'] = $_POST['dbname'];
$_SESSION['username'] = $_POST['username'];
$_SESSION['pw'] = $_POST['pw'];
?>
<?php endif ?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Login Test</title>
</head>
<body>
<?
if (isset($_POST['return']))
{
$_SESSION['session_level'] = 0;
}
else if (isset($_POST['submit']))
{
try
{
$db = new PDO("mysql:host=".$_POST['host'].";dbname=".$_POST['dbname'], $_POST['username'], $_POST['pw']);
}
catch(Exception $error)
{
$_SESSION['session_level'] = 0;?>
<a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a>
<? echo "\n"; ?>
<?die("Connection to user database failed: " . $error->getMessage());
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO:: ERRMODE_EXCEPTION);
$query = "SHOW TABLES";
$results = $db->query($query)->fetchAll();
$_SESSION['session_level'] = 1;
}
catch(Exception $error)
{
echo "Problem with query!";
$_SESSION['session_level'] = 0;?>
<a href='<?= $_SERVER['PHP_SELF'] ?>'>Click here to return.</a>
<? }
}
?>
<?php if($_SESSION['session_level'] == 0){?>
<h1>Database Practice</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='initialentry'>
<table border='0' style='text-align: center'>
<tr>
<td style='text-align: right;'>Enter host name:</td>
<td style='text-align: left;'>
<input type='text' name='host' value='localhost'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter database name:</td>
<td style='text-align: left;'>
<input type='text' name='dbname' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter user name:</td>
<td style='text-align: left;'>
<input type='text' name='username' value='zxyx999'>
</td>
</tr>
<tr>
<td style='text-align: right;'>Enter password:</td>
<td style='text-align: left;'>
<input type='password' name='pw' width='15' value='12345'>
</td>
</tr>
<tr>
<td style='text-align: right;'><input type="reset" name="reset" value="Reset"></td>
<td style='text-align: left;'><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
<?php }
else if ($_SESSION['session_level'] == 1)
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<select name='select'>
<? foreach($results as $row)
echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?>
</select>
<input type="submit" name="selected" value="Select">
<input type="submit" name="return" value="Return to Main Screen">
</form>
<?php
if(isset($_POST['selected']))
{
try
{
$db = new PDO("mysql:host=".$_SESSION['host'].";dbname=".$_SESSION['dbname'], $_SESSION['username'], $_SESSION['pw']);
}
catch(Exception $error)
{
die("Connection to user database failed: " . $error->getMessage());
}
try
{
$query = $db->prepare("SELECT * FROM " . $_POST['select']);
$query->execute();
$header = true;
}
catch(Exception $error)
{
echo "Query failed.";
}
echo "</br>";
?>
<?php
echo "<table border='1'>";
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
if($header == 'true')
{
foreach($row as $index => $fieldValue)
{
echo "<td>";
echo $index;
echo"</td>";
}
echo "</tr>";
$header = 'false';
}
echo "<tr>";
foreach($row as $index => $fieldValue)
{
echo "<td>";
echo $fieldValue;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
</body>
</html>
If I well understand your problem then my advice 'll be to change your form and do it this way :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<input type="hidden" name="selected" value="true">
<select name='select'>
<? foreach($results as $row)
echo "<option value=" . $row[0] .">" .$row[0]. "</option>"; ?>
</select>
<input type="submit" value="Select">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name='getForm'>
<input type="hidden" name="return" value="true">
<input type="submit" value="Return to Main Screen">
</form>
It means you must create 2 forms instead of 1 with because the name value on a submit button is not intrepreted the same way on all browser. So by removing and replacing it by an input type hidden it 'll assure the field 'll exist after submitting forms.
When you repopulate the select list, if the option selected matches the option being populated, then you can make that bit of code <option selected value=" and that option will now be the default selected item at the moment.
I realize the below code isn't PHP, my point is that it should be a simple If statement, to include the selected tag into the option.
/*
Adding the "selected" tag to an option, makes it the default.
*/
<select>
<option value="" style="display:none;">Select a Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br /><br />
<select>
<option value="" style="display:none;">Select a Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
</select>