0

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

<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 旁边。据我所知,它没有保留 的POSTSPKSelect。这与我的事实有关吗? 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>&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>
4

2 回答 2

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 目录中,并将其包含在 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 模板)

于 2012-10-02T11:53:14.123 回答
1

为什么你有以下内容:

$date = $_POST['DateSelect'];

if ($selectedSPK); // The semi colon should not be there
{
于 2012-10-02T12:03:05.700 回答