简体   繁体   English

动态填充PHP中的下拉列表

[英]Populating a Dropdown list in PHP dynamically

I have a small PHP page which contains two drop down lists 我有一个小的PHP页面,其中包含两个下拉列表

I need to populate the second one according to the result selected in the first drop down list .... is this possible? 我需要根据第一个下拉列表中选择的结果填充第二个....这可能吗? In other words I need to use the value selected from the first drop down list and use it in the dB query used to populate the second drop down list (but this should be populated upon selection of the first drop down list. 换句话说,我需要使用从第一个下拉列表中选择的值,并在用于填充第二个下拉列表的dB查询中使用它(但是这应该在选择第一个下拉列表时填充。

If this is possible any hints please? 如果有可能有任何提示吗? (you can assume that I am able to populate the first drop down list from the dB) (你可以假设我能够从dB填充第一个下拉列表)

thanks 谢谢

Option 1: embed the data for the second select in the document as hidden elements or JS objects. 选项1:将文档中第二个选择的数据嵌入为隐藏元素或JS对象。 A change event handler on the first select will populate the second select. 第一个选择上的change事件处理程序将填充第二个选择。 A List Apart has an example dynamic select page . List Apart有一个动态选择页面示例

Option 2: use AJAX. 选项2:使用AJAX。 When the first select changes, make a request to the server for the contents of the second select, then populate it. 当第一个选择更改时,向服务器请求第二个选择的内容,然后填充它。 With a JS library (such as jQuery), this becomes quite easy. 使用JS库(例如jQuery),这变得非常简单。

$('select#one').change(
  function (evt) {
    $('select#two').load('/thing/'+this.value);
  }
);

"/thing/<val>" identifies your server side script to generate a list of items based on "<val>" (use the rewrite facilities of your webserver to resolve the URL path to the actual script). “/ thing / <val>”标识服务器端脚本以基于“<val>”生成项目列表(使用Web服务器的重写功能来解析实际脚本的URL路径)。 You could simply have it always generate <option> elements, but a better design would be to include a way to specify the result format, so it could output the list as <li>, using JSON or some other format. 您可以简单地让它始终生成<option>元素,但更好的设计是包含一种指定结果格式的方法,因此它可以使用JSON或其他格式将列表输出为<li>。

$('select#one').change(
  function (evt) {
    $('select#two').load('/thing/'+this.value, {fmt: 'option'});
  }
);

You will have to use AJAX to send the selection of the first dropdown to the server. 您必须使用AJAX将第一个下拉列表的选择发送到服务器。 You can then query the database and generate the second dropdown and send it back to the user. 然后,您可以查询数据库并生成第二个下拉列表并将其发送回用户。

@outis has good point use .change in jquery otherwise use onchange event in select code. @outis有很好的用途.change在jquery中使用。否则在select代码中使用onchange事件。

like 喜欢

<select id='my_select' onchange='javascript:myfunc()'>
<option value='a'>a</option>
.
.
<option value='z'>z</option>

function myfunc(){
//write code to populate another dropdown based on selected value

}

You can see this Dynamically Populating Dropdown Based On Other Dropdown Value 您可以看到此基于其他下拉值的动态填充下拉列表

You'll need an asynchronous call back to the server, without a page reload. 您需要异步回调服务器,而不需要重新加载页面。 (I doubt that you actually want to have a button that posts back to the server) So AJAX is something that can do exactly that. (我怀疑你真的想要一个回复服务器的按钮)所以AJAX可以做到这一点。 Add an AJAX call to your first dropdown's onchange event handler that posts the selection back to the server and returns the contents of the second dropdown. 将AJAX调用添加到第一个下拉列表的onchange事件处理程序,该处理程序将选择发布回服务器并返回第二个下拉列表的内容。 When the AJAX call returns the new values, you will use it to build your content for the second dropdown. 当AJAX调用返回新值时,您将使用它来构建第二个下拉列表的内容。 Most of this is done in Javascript, of course, besides the actual server part, which will remain in PHP. 当然,大部分内容都是在Javascript中完成的,除了实际的服务器部分,它将保留在PHP中。

There's two ways of doing it. 有两种方法可以做到这一点。 The old-school "select an option and submit to rebuild the page" method, which works pretty much universally, and the newfangled AJAX methods, to load the second dropdown's contents without refreshing the page. 旧学校“选择一个选项并提交以重建页面”方法(几乎普遍适用)和新奇的AJAX方法,在不刷新页面的情况下加载第二个下拉列表的内容。

Both have advantages/disadvantages, so it boils down to what's best for your purposes. 两者都有优点/缺点,因此它归结为最适合您的目的。 The oldschool method doesn't require any javascript at all, but since it does round-trip the form through the server, you'll get the "clear the window and then redraw the page" flickering. oldschool方法根本不需要任何javascript,但由于它通过服务器往返表单,你将得到“清除窗口,然后重绘页面”闪烁。

The AJAX method bypasses the refresh flicker, but also would no work on Javascript-disabled browsers. AJAX方法绕过刷新闪烁,但也不适用于禁用Javascript的浏览器。 It does require a little bit more code, client-side, to handle the AJAX calls and population of the dropdown, but the server-side code would be pretty much the same for both methods: same queries, same retrieval loops, just different output methods. 它需要更多的代码,客户端,来处理AJAX调用和下拉列表,但服务器端代码对于两种方法几乎相同:相同的查询,相同的检索循环,只是不同的输出方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM