我有以下代码来保留选择框的值,该选择框在更改时提交表单。
<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>
这不起作用,它只是默认回到原来的 --Select Ref--- 选项。
我有这个确切的代码适用于其他选择框,'form1'
但是这是“form2”中的第一个,它位于表单 1 旁边。据我所知,它没有保留 的POST
值SPKSelect
。这与我的事实有关吗? m 使用 2 个表单?POST 是否仅适用于每页 1 个表单?如果可以,我该如何解决?
完整页面如下:
<!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>