-1

我编写了一个代码,我试图通过从不同的下拉列表中选择选项来填充下拉列表。我用谷歌搜索并发现这可以通过 javascript 实现。onchange()但是在第一个组合框的方法中将 php 数组传递给 javascript 时出现错误。数组非常好,它给了我想要的结果,但是当我尝试填充它时会发生错误......它显示未初始化的变量错误......但我无法找到为其设置值的方法。

 <?php
    session_start(); // NEVER forget this!
    if(!isset($_SESSION['loggedin']))
    {
        die("To access this page, you need to <a href='main_login.php'>LOGIN</a>"); 
    } 
?>
<html>
 <head>
  <script type="text/javascript">
    function validateForm()
    {
    var name=document.forms["form1"]["name"].value;
    var passno=document.forms["form1"]["passno"].value;
    var applicationno=document.forms["form1"]["applicationno"].value;
    var price=document.forms["form1"]["price"].value;
    var date=document.forms["form1"]["date"].value;
    var reg = /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/;
    if ((name=="" || name==null) || (passno=="" || passno==null) || (applicationno=="" || applicationno==null || isNaN(applicationno) ) || (price=="" || price==null || isNaN(price)) || (date=="" || date==null || date=="00-00-0000" || !date.match(reg)))
      {
      alert("Applicant Name, Passport Number, Application Number,Price And Date Must Be Filled Out.\n\n Valid Date Format Is : (dd-mm-yyyy) \n\n Only numbers are allowed for Application Number and Price.");
      return false;
      }
      else {
          return true;

            }
    }
    function fillDate(array1,array2)
    {
        document.form1.subagentname.options.add( new Option(array1,array2) );
        }
  </script>

    <style type="text/css">
    input.uppercase { 
    text-transform: uppercase; 
    }
    </style>
 </head>
 <body>
    <h1 align="center">ADD VISA DETAILS</h1>
    <form id="form1" name="form1" method="post" action="insertvisa.php" onSubmit="return validateForm();">
    <p>
        <label for="name">APPLICANT NAME : * </label>
        <input type="text" name="name" id="name" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
      </p>

      <p>
        <label for="company">COMPANY : </label>
        <select name="company" id="company">
          <option value="TULIP TOURISM LLC">TULIP TOURISM LLC</option>
          <option value="TULIP TRAVELS LLC">TULIP TRAVELS LLC</option>
          <option value="EXCELLENT MARINA">EXCELLENT MARINA</option>
        </select>
      </p>
      <p>
        <label for="nationality">NATIONALITY : </label>
        <input type="text" name="nationality" id="nationality" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
      </p>
      <p>
        <label for="passno">PASSPORT NUMBER : * </label>
        <input type="text" name="passno" id="passno" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
      </p>
      <p>
        <label for="applicationno">APPLICATION NUMBER : * </label>
        <input type="text" name="applicationno" id="applicationno" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
      </p>
      <p>
        <label for="proctype">PROCEDURE TYPE : </label>
        <select name="proctype" id="proctype">
          <option value="NORMAL">NORMAL</option>
          <option value="URGENT">URGENT</option>
        </select>
      </p>
      <p>
        <label for="visatype">VISA TYPE : </label>
        <select name="visatype" id="visatype">
          <option value="TOURIST">TOURIST</option>
          <option value="SERVICE">SERVICE</option>
          <option value="TT">TT</option>
          <option value="EXTENSION">EXTENSION</option>
          <option value="CANCELLATION">CANCELLATION</option>
        </select>
      </p>
      <p>
        <label for="agent">AGENT : </label>
        <?php 

    $server="localhost";
    $username="root";
    $password="hp";
    $link=mysql_connect($server, $username, $password) or die ("Cannot connect to mysql server: ".mysql_error());

    $dbname = 'apcom_visa_system';
    mysql_select_db($dbname, $link) or die ("Cannot connect to database: ".mysql_error());

    $query="SELECT agent_id,agent_name FROM agent";

    $result = mysql_query ($query) or die ('error submitting');

    echo "<select name='agentname' id='agentname' onChange='return fillData(htmlspecialchars(json_encode($subagentsID), ENT_NOQUOTES),htmlspecialchars(json_encode($subagentsName), ENT_NOQUOTES));'>";
    while($drop=mysql_fetch_array($result)){

    //data stored in $drop
    echo "<option value='$drop[agent_id]'>$drop[agent_name]</option>";
    $agentsID[]=$drop[agent_id];

    }
    echo "</select>"; 

    foreach($agentsID as $values)
    {
        //echo "agents-->".$values;

    $query1="SELECT subagent_id,subagent_name FROM subagent WHERE agent_id=$values";

    $result1 = mysql_query ($query1) or die ('error submitting');

    while($drop1=mysql_fetch_array($result1)){
    $subagentsID[]=$drop1['subagent_id'];
    $subagentsName[]=$drop1['subagent_name'];

        //echo "subagents-->".$drop1['subagent_id'];
    }


        }


    mysql_close($link);
    ?>
      </p>
      <p>
      <label for="subagentname">SUBAGENT : </label>
      <select name="subagentname" id="subagentname"></select>
      </p>

      <p>
        <label for="cost">COST : </label>
        <select name="cost" id="cost">
          <option value="210">210</option>
          <option value="310">310</option>
          <option value="610">610</option>
          <option value="90">90</option>
        </select>
      </p>
      <p>
        <label for="price">PRICE : * </label>
        <input type="text" name="price" id="price" style="text-transform:uppercase;" onKeyUp="this.value=this.value.toUpperCase()"/>
        <label for="currency"></label>
        <select name="currency" id="currency">
          <option value="AED">AED</option>
          <option value="PKR">PKR</option>
        </select>
      </p>
      <p>
        <label for="status">STATUS : </label>
        <select name="status" id="status">
          <option value="APPLICATION NOT POSTED">APPLICATION NOT POSTED</option>
          <option value="APPLICATION POSTED">APPLICATION POSTED</option>
          <option value="APPLICATION INPROCESS">APPLICATION INPROCESS</option>
          <option value="APPLICATION REJECTED">APPLICATION REJECTED</option>
          <option value="APPLICATION APPROVED">APPLICATION APPROVED</option>
          <option value="DOCUMENTS REQUIRED">DOCUMENTS REQUIRED</option>
        </select>
      </p>
      <p>
        <label for="date">APPLICATION DATE : * </label>
        <input type="text" name="date" id="date">
    (DATE FORMAT : dd-MM-yyyy , Example : 11-05-2012) </p>
      <p>
      <p>
      * Required
      </p>
        <input type="submit" name="submit" id="submit" value="Submit" />
        <input type="submit" name="cancel" id="cancel" value="Cancel" onClick="document.location.href='main.php';return false;"/>
      </p>
  </form>
 </body>
</html>
4

1 回答 1

0

不要使用 php 更改第二个下拉列表,而是使用 javascript 更改它。在更改第一个下拉列表时,对页面进行 ajax 调用(这与您现在为获取第二个下拉列表的数据所做的操作相同,而不是将其放在同一页面上创建一个返回选项列表的新页面对于第二个下拉菜单),它为您提供了必须在第二个下拉菜单中显示的选项列表。

于 2012-12-27T17:07:56.017 回答