简体   繁体   English

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

[英]How to get the second value in a dropdown box based on the selected one in the first dropdown box

I have written the following code in PHP to generate two dropdown boxes on the same page. 我在PHP中编写了以下代码,以在同一页面上生成两个下拉框。

The first dropdown box gets value from a MySQL table. 第一个下拉框从MySQL表中获取值。 This dropdpwn box contains some UserIDs. 此dropdpwn框包含一些UserID。 The second dropdown box contains some dates which should be based on the UserID that is selected in the first dropdown box. 第二个下拉框包含一些日期,这些日期应基于在第一个下拉框中选择的UserID。 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. 我在第二个下拉框中填入了MySQL表中的所有日期,但是,应该在第一个下拉框中选择的UserID进行过滤。

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. 只是为了通知这个PHP页面中这两个下拉框中的这两个值,我通过按下提交按钮到另一个PHP页面来处理其他一些工作。

I would appreciate if you can help me to fill the second dropbox only based on the UserID selected on the first dropbox. 如果您能够帮助我仅根据第一个保管箱上选择的用户ID填充第二个保管箱,我将不胜感激。 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. 我是PHP的新手,这就是我要求代码级帮助的原因。

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'>\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>

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. 你需要的是AJAX。 The easiest way to implement it is probably jQuery ajax function . 实现它的最简单方法可能是jQuery ajax函数 If you don't know jQuery - learn the basics, it shouldn't take more than an hour or so. 如果你不了解jQuery - 学习基础知识,它不应该花费超过一个小时左右。 It's worth it :) 这很值得 :)

You'll need 3 things 你需要3件事

  1. Initial page with select fields 包含选择字段的初始页面
    • The first select field is pre-populated with user ids 第一个选择字段预先填充了用户ID
    • 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 一个单独的端点/页面,它将用户ID作为参数来返回相关日期
    • 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) 它可能应该返回JSON / XML(或类似的东西),或者你可以返回预先在<option />标签中呈现的日期(不应该真的这样做,但是一起破解它会更快)
  3. Javascript callback triggered when an option in the first dropdown is selected. 选择第一个下拉列表中的选项时会触发Javascript回调。 The callback should send an AJAX request to the separate endpoint and populate the second dropdown with the result. 回调应该向单独的端点发送AJAX请求,并使用结果填充第二个下拉列表。

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. 只是尝试设置这一切(都是它;相对简单)将教你一大堆关于Javascript,AJAX和Web服务的东西。

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. 如果您选择从Web服务(单独的端点/页面)返回JSON / XML,并且希望您能够,那么您也可能会开始看到将逻辑与表示分离的好处,这将使您的理解世界变得不同和交付代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文本框中获取与下拉列表相关的选定值 - Get the selected dropdown related value into the text box 通过jQuery Ajax创建的下拉列表:如何在第一个下拉列表中的选定值的第二个下拉列表中获取相关项 - Dropdown created via jquery ajax:how to get related items in second dropdown of selected value in first dropdown PHP:下拉框中的选定值 - PHP: selected value in dropdown box 如何从下拉列表中检索选定的值并将其显示在同一个下拉框中 - How to retrieve the selected value from dropdown and display it in the same dropdown box 如果在第一个下拉菜单中选择了某个值,如何显示第二个下拉菜单? - How to display a second dropdown menu if a certain value in the first dropdown is selected? 根据选择的第一个下拉列表获取下拉值 - Get Values of Dropdown based on First Dropdown selected PHP-获取在下拉框中选择的选项的索引/关联值 - PHP- Get index/associated value of option selected in dropdown box 选定的值从数据库获取到下拉选择框 - Selected value get from DB into dropdown select box 从下拉框中获取选定值的下两个值 - Get the next 2 values of a selected value from dropdown box 需要在多个下拉框中选择选定的下拉框值 - Selected Dropdown Box values needs to get selected in multiple dropdown boxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM