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

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

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzen2675 2012-03-19 00:32

    Well, we wont write the code for you. Otherwise you are going to be newbie for all your life :)

    What you need here is called AJAX. The easiest way to implement it is probably jQuery ajax function. If you don't know jQuery - learn the basics, it shouldn't take more than an hour or so. It's worth it :)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题