I have the following code to retain the value of a select box, which is submitting the form on change.
<form id="form2" name="form2" method="post" action="index.php" >
<table width="98%" border="0">
<tr>
<td>SPAK Ref</td>
<td><select name="SPKSelect" id="SPKSelect" onchange="document.forms[0].submit();document.forms['form2'].submit(); ">
<option value="">-- Select Ref --</option>
<?php
$selectedSPK = $_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK);
{
$SPKquery = "SELECT DISTINCT SPKCustNo FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ORDER by RenewalDate";
$SPKresult = mysql_query($SPKquery);
while($row = mysql_fetch_array($SPKresult))
{
echo "<option value=\"".$row['SPKCustNo']."\">".$row['SPKCustNo']."</option>\n ";
}
}
?>
<script type="text/javascript">
document.getElementById('SPKSelect').value = <?php echo json_encode(trim($selectedSPK));?>;
</script>
</select>
And this isn’t working, it just defaults back to the origianl –Select Ref— option.
I have this exact code working for other selectboxes in 'form1' however this is the first one in “form2′ which sits next to form 1. From what I can tell it’s not retaining the POST value of SPKSelect. Is this something to do with the fact that I’m using 2 forms? Does POST only work on 1 form per page? If so how can I get around this?
The full page is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>TIL / 006 Renewal Support Tool</title>
<link href="RenewalToolStyles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/functions.js"></script>
</head>
<?php
//Connect to Server / Database
$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("TILRenewals");
function GenerateTable($Query)
{
$host = "localhost";
$user = "root";
$pass = "";
$db = "TILRenewals";
$link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error()); //build MySQL Link
mysql_select_db($db) or die('Could not select database'); //select database
$Table = ""; //initialize table variable
$Table.= "<table border='1' style=\"border-collapse: collapse; text-align: center; font-size: small; cellspacing: 10px; \">"; //Open HTML Table
$Result = mysql_query($Query); //Execute the query
if(mysql_error())
{
$Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
}
else
{
//Header Row with Field Names
$NumFields = mysql_num_fields($Result);
$Table.= "<tr style=\"background-color: #000; text-align: center; color: #FFFFFF; font-size: medium;\">";
for ($i=0; $i < $NumFields; $i++)
{
$Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
}
$Table.= "</tr>";
//Loop thru results
$RowCt = 0; //Row Counter
while($Row = mysql_fetch_assoc($Result))
{
//Alternate colors for rows
if($RowCt++ % 2 == 0) $Style = "background-color: #3D6522; color: #FFFFFF;";
else $Style = "background-color: #FFFFFF; color:#3D6522;";
$Table.= "<tr style=\"$Style\">";
//Loop thru each field
foreach($Row as $field => $value)
{
$Table.= "<td>$value</td>";
}
$Table.= "</tr>";
}
// $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
}
$Table.= "</table>";
return $Table;
}
?>
<body>
<div class="container">
<div class="header"><img src="Images/Banner.png" width="728" height="90" /><!-- end .header --></div>
<div class="content">
<div id ="leftcolumn">
<h2>View Assigned Cases</h2>
<form id="form1" name="form1" method="post" action="index.php">
<p>Sales Exec
<select name="Sales_Exec" id="Sales_Exec" onchange="document.forms[0].submit();">
<option value="">-- Select SE --</option>
<?php
$salesexec=$_POST['Sales_Exec'];
if ($salesexec);
{
$Execquery = "SELECT DISTINCT Assigned FROM Data";
$Execresult = mysql_query($Execquery);
while($row = mysql_fetch_array($Execresult))
{
echo "<option value=\"".$row['Assigned']."\">".$row['Assigned']."</option>\n ";
}
}
?>
<script type="text/javascript">
document.getElementById('Sales_Exec').value = <?php echo json_encode(trim($_POST['Sales_Exec']));?>;
</script>
</select>
Date
<select name="DateSelect" id="DateSelect" onchange="document.forms[0].submit();">
<option value="">-- Select Date --</option>
<?php
$selecteddate=$_POST['DateSelect'];
if ($selecteddate);
{
$Datequery = "SELECT DISTINCT RenewalDate FROM Data ORDER by RenewalDate";
$Dateresult = mysql_query($Datequery);
while($row = mysql_fetch_array($Dateresult))
{
echo "<option value=\"".$row['RenewalDate']."\">".$row['RenewalDate']."</option>\n ";
}
}
?>
<script type="text/javascript">
document.getElementById('DateSelect').value = <?php echo json_encode(trim($_POST['DateSelect']));?>;
</script>
</select>
</p>
<p>
<?php
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
echo GenerateTable("SELECT SPKCustNo, RenewalDate, Product, ForeName, Surname FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ");
?>
</p>
</form>
<p> </p>
</div>
<div id ="rightcolumn">
<h2>View Individual Case</h2>
<form id="form2" name="form2" method="post" action="index.php" >
<table width="98%" border="0">
<tr>
<td>SPAK Ref</td>
<td><select name="SPKSelect" id="SPKSelect" onchange="submit_form('form1'); submit_form('form2'); ">
<option value="">-- Select Ref --</option>
<?php
$selectedSPK = $_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
$SPKquery = "SELECT DISTINCT SPKCustNo FROM Data WHERE Assigned = '$assigned' AND RenewalDate = '$date' ORDER by RenewalDate";
$SPKresult = mysql_query($SPKquery);
while($row = mysql_fetch_array($SPKresult)) {
if($row["SPKCustNo"] == $selectedSPK){
$select_true = "selected='selected'";
}
print "<option " . $select_true . " value='" .$row["SPKCustNo"] . "'>" . $row["SPKCustNo"] . "</option>";
}
?>
<script type="text/javascript">
document.getElementById('SPKselect').value = <?php echo json_encode(trim($_POST['SPKSelect']));?>;
</script>
</select>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td width="33%">ForeName</td>
<td><select name="FName" id="FName">
<option value="">--Customer ForeName---</option>
<?php
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
$selectedSPK = $_POST['SPKSelect'];
if ($selectedSPK) {
$FNamequery = "SELECT ForeName FROM Data WHERE SPKCustNo = '$selectedCust'";
$FNameresult = mysql_query($FNamequery);
while($row = mysql_fetch_array($FNameresult))
{
echo "<option value=\"".$row['ForeName']."\">".$row['ForeName']."</option>\n ";
}
}
?>
<script type="text/javascript">
document.getElementById('FName').value = <?php echo json_encode(trim($_POST['FName']));?>;
</script>
</select></td>
</tr>
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<p> </p>
</form>
<p> </p>
</div>
<!-- end .content --></div>
<div class="footer">
<p>Footer<img src="Images/TILLogo.png" width="150" height="79" /></p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
IMPORTANT EDIT:
I have overlooked one big thing!
When you are submitting forms only the fields from current form are sent trough the request.
There is one workaround, you can have hidden fields in current form that corresponds to inputs from first form and use javascript on before submit to copy values from first form to current hidden elements. And then submit current form.
Possible error in onchange:
Change it to:
And when you do an mysql_fetch do this so that you can know what is the current data so that you can select it, like this:
Also like Laurent said you need to remove semi colon from this line:
it should look like this:
EDIT JAVASCRIPT TIP:
Create external .js file for example “workers.js” put it in for example js dir and include it in head of HTML like this:
Then write your javascript functions in that file first function is this for submiting forms:
Then call it from HTML onchange like this:
This is much cleaner method and longterm it will give you readable code.
Also try: http://www.smarty.net (for php templating)