如何基于第一个下拉框中的所选值获得下拉框中的第二个值 [英] How to get the second value in a dropdown box based on the selected one in the first dropdown box

查看:71
本文介绍了如何基于第一个下拉框中的所选值获得下拉框中的第二个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用PHP编写了以下代码,以在同一页面上生成两个下拉框.

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

第一个下拉框从MySQL表获取值.此dropdpwn框包含一些用户ID.第二个下拉框包含一些日期,这些日期应基于在第一个下拉框中选择的UserID.我已经用MySQL表中的所有日期填充了第二个下拉框,但是,应该使用在第一个下拉框上选择的UserID对其进行过滤.

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.

仅通过此PHP页面上这两个下拉框中的这两个值来告知,我已经通过按下Submit按钮将其发布到另一个PHP页面上来处理其他工作,来将它们发布.

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.

如果您能帮助我仅根据在第一个保管箱上选择的UserID填写第二个保管箱,将不胜感激.这是我编写的用于显示和填充这些下拉框的代码.您能告诉我我应该修改代码的哪一部分,如果您也能向我展示修改代码,我将不胜感激.我是PHP的新手,所以才需要代码级帮助.

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.

我的代码:

<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'>\n";
//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>

推荐答案

您将需要三件事

  1. 具有选择字段的初始页面
    • 第一个选择字段已预先填充了用户ID
    • 第二个字段不包含任何选项,并且已禁用
  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
  • 它可能应该返回JSON/XML(或类似的东西),或者您可以返回在<option />标记中预先渲染的日期(本来不是应该这样做的,但是将其组合在一起会更快).
  • 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)

与[try]解释相比,对我来说,为您全部编写出来可能会容易一些,但这并不是重点.仅仅尝试设置所有内容(全部;相对简单)就可以教会您有关Javascript,AJAX和Web服务的全部内容.

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.

如果您选择从Web服务(单独的终结点/页面)返回JSON/XML,并且希望您会这样做,那么您可能还会开始看到将逻辑与表示分离的好处,这将给您带来与众不同的感觉.您对代码的理解和交付.

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.

这篇关于如何基于第一个下拉框中的所选值获得下拉框中的第二个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆