选择框不保留值

时间:2012-10-02 11:46:37

标签: php javascript mysql

我有以下代码来保留选择框的值,即在更改时提交表单。

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

这不起作用,它只是默认返回原始 - 选择参考---选项。

我有这个确切的代码适用于'form1'中的其他选择框但是这是第一个位于表单1旁边的“form2”。据我所知,它不保留POSTSPKSelect。这与我使用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>&nbsp;</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>&nbsp;</td>
        <td>&nbsp;</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>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
  </table>
    <p>&nbsp;</p>
  </form>
  <p>&nbsp;</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>

2 个答案:

答案 0 :(得分:1)

重要编辑:

我忽略了一件大事!

当您提交表单时,只会通过请求发送当前表单中的字段。

有一种解决方法,您可以在当前表单中使用与第一个表单的输入相对应的隐藏字段,并在提交之前使用javascript将第一个表单中的值复制到当前隐藏的元素。然后提交当前表格。

onchange可能出错:

onchange="document.forms[0].submit();document.forms['form2'].submit(); "

将其更改为:

onchange="document.forms['form2'].submit();"

当您执行mysql_fetch时,请执行此操作以便您可以知道当前数据是什么,以便您可以选择它,如下所示:

    while($row = mysql_fetch_array($SPKresult)) {

    if($row["SPKCustNo"] == $selectedSPK){

         $select_true = "selected='selected'";

    }

         print "<option " . $select_true . " value='" .$row["SPKCustNo"] . "'>" . $row["SPKCustNo"] . "</option>";

    }

同样Laurent说你需要从这一行中删除半结肠:

if ($selectedSPK);
{

它应该是这样的:

if ($selectedSPK) {

编辑JAVASCRIPT提示:

创建外部.js文件,例如“workers.js”将其放入例如js dir中并将其包含在HTML的头部中,如下所示:

<script type="text/javascript" src="js/workers.js"></script>

然后在该文件中编写您的javascript函数第一个函数用于提交表单:

function submit_form(formid) {

    document.getElementById(formid).submit();

}

然后从HTML onchange中调用它:

onchange="submit_form('form_id');"

这是一种更干净的方法,长期以来它会为您提供可读的代码。

还可以尝试:http://www.smarty.net(用于php模板)

答案 1 :(得分:1)

为什么你有以下内容:

$date = $_POST['DateSelect'];

if ($selectedSPK); // The semi colon should not be there
{
相关问题