doutong4088 2012-03-18 18:37
浏览 265
已采纳

如何根据第一个下拉框中选定的值在下拉框中获取第二个值

I have written the following code in PHP to generate two dropdown boxes on the same page.

The first dropdown box gets value from a MySQL table. This dropdpwn box contains some UserIDs. The second dropdown box contains some dates which should be based on the UserID that is selected in the first dropdown box. I have filled the 2nd dropdown box with all the dates in the MySQL table, but, it should be filtered by the UserID which is selected on the first dropdown box.

Just to inform, with these two values from these two dropdown boxes in this PHP page, I have posted them by pressing the submit button to another PHP page to process some other work.

I would appreciate if you can help me to fill the second dropbox only based on the UserID selected on the first dropbox. Here is the code I have written to display and fill those dropdown boxes. Can you please inform me, what part of the code I should modify and I would appreciate if you can show me the modification code as well. I am a newbie in PHP, that's why I am asking for code level help.

My code:

<html>
<head>
<title>
 Search Alert DB
</title>
<body>

  <br />

<?php>

  $con = mysql_connect("localhost","root","root"); // (host, user,pwd)
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mysql", $con);

echo "<p> Search Alert database </p>";
echo "<br />";

$result = mysql_query("SELECT distinct(UserID) FROM tblAlertLogSpecificUsersDayStatusFinal_1");
$options="";

echo "<form action='Search_AlertDB_process.php' method='POST'>
";
//echo "Please choose a user: ";
echo "Please choose a user: <select name = userid>";
echo "<option>-Select-";
while ($row = mysql_fetch_array($result))
{

$userid=$row["UserID"]; 
$options ="<option value = \"$userid\">$userid </option>";
echo "$options";

}
echo "</select>";
echo "<br />";
echo "<br />";
$dayresult = mysql_query("SELECT distinct(Occurred_date) FROM tblAlertLogSpecificUsersDayStatusFinal_1");
$dayoptions="";

echo "Please pick a date:<select name = day>";
echo "<option>-Select-";
while ($row=mysql_fetch_array($dayresult)) { 

    $day=$row["Occurred_date"]; 
    $dayoptions ="<option value = \"$day\">$day </option>";
    echo "$dayoptions";
    //$options.="<OPTION VALUE=\"$id\">".$day; 
}   
echo "</select>";
echo "<br />";  
mysql_close($con);  
?>  

<br />
<input type="submit" name="Submit" value="Search" /> <br /> <br />
</form>

</body>
</html>
  • 写回答

2条回答 默认 最新

  • druhoytza979667566 2012-03-19 00:51
    关注

    You'll need 3 things

    1. Initial page with select fields
      • The first select field is pre-populated with user ids
      • The second field contains no options and is disabled
    2. A separate endpoint/page that takes a user id as a parameter to return relevant dates
      • It should probably return JSON/XML (or something similar), or you could return the dates pre-rendered in <option /> tags (shouldn't really do this, but it would be quicker to hack this together)
    3. Javascript callback triggered when an option in the first dropdown is selected. The callback should send an AJAX request to the separate endpoint and populate the second dropdown with the result.

    It would have probably been easier for me to write it all out for you than [try] to explain it, but that's not really the point. Just trying to set this all up (all be it; relatively simple) will teach you a whole load of things about Javascript, AJAX and web services.

    If you choose to return JSON/XML from your web service (the separate endpoint/page), and hopefully you will, you might also start to see the benefit of separating logic from presentation, which will make the world of difference to both your understanding and delivery of code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!