在 PHP 中拥有动态下拉列表和 POST 选择值的最佳方法是什么
我正在用 PHP 编写以下内容: 我想要 2 个下拉列表,其中第二个下拉列表是根据第一个下拉列表中所做的选择从 mySQL dB 填充的。然后我想使用下拉列表中所选值的值来调用另一个 PHP 页面,我将在其中生成数据库查询。
例如: 名单A:汽车制造商(本田、日产等) 列表 B:模型(accord、civic 等)
然后我将有一个提交按钮,用于将值发布到另一个 PHP 文件中? (我有如何在没有动态列表的情况下执行上述操作的基础知识,但我使用 AJAX 尝试过此操作,但在传递第二个下拉列表的值时遇到问题)有关
如何执行上述操作的任何提示?
I am writing the following in PHP:
i would like to have 2 dropdownlists, where the second one is populated from a mySQL dB according to the choice made on the first dropdown list. Then I would like to use the values of the selected values of the dropdownlist to call another PHP page in which I will generate a db query.
Eg:
List A: Car Manufactures (honda, nissan, etc)
List B: Models (accord, civic, etc)
Then I will have a submit button, to POST the values in another PHP file? (I have the basics of how to perform the above without having a dynamic list, but I tried this with AJAX, but I am having problems to pass the value of the second dropdownlist)
Any tips of how I can perform the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该有一个单独的 PHP 文件(我们称之为
search.php
),它将采用 POST 变量(我们称之为make
)并运行一个查询,该查询将输出列表。在它输出列表后(每行一个
model
,以\n
结尾),您应该有一些 javascript,然后将其放入第二个下拉列表中。点击提交按钮应该只是将表单 POST 到另一个 PHP 文件。
You should have a separate PHP file (let's call it
search.php
) which will take a POST variable (let's call itmake
) and run a query which will spit out a list.After it spits out the list (one
model
per line, ends with a\n
) you should have a bit of javascript which will then throws that into the second drop down.Hitting the submit button should just be a form POST to another PHP file.
我建议首先不要使用 AJAX。
只需使用 GET 方法制作第一个表单即可。因此,提交此表单后,您将在第二个脚本上看到经典的 ?choice=1 查询字符串。因此,您可以使用 $_GET['choice'] (当然经过适当清理)来查询数据库并填充第二个选择。
不要忘记添加
到第二个表格
I'd suggest to make it without AJAX at first.
just make a first form using GET method. So, after submit this form, you'll land with a classical ?choice=1 query string on the second script. So, you can use $_GET['choice'] (properly sanitized of course) to query a database and populate second select.
Don't forget to add
to the second form
在这里找到解决方案:
http://www.plus2net.com/php_tutorial/php_drop_down_list.php
solution found here:
http://www.plus2net.com/php_tutorial/php_drop_down_list.php