使用 php 和 javascript 使用 mysql 表值动态填充组合框

发布于 2024-11-17 22:59:47 字数 112 浏览 9 评论 0原文

我想根据在另一个组合框中选择的值有条件地在运行时使用 mysql 表值填充组合框。是否可以仅使用 PHP 来实现,还是需要一些客户端脚本语言(例如 Javascript)?

请帮忙看一下代码..

I want to populate a combo-box on runtime with mysql table values, conditionally, on the basis of a value selected in another combo-box . Can this be implemented by only using PHP, or will i need some client side scripting language such as Javascript?

Please help with code..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-11-24 22:59:47

是的,可以使用 javascript 和 PHP。您的 JavaScript 将使用 AJAX 查询 PHP 脚本,为 PHP 脚本提供第一个组合框的值。然后,PHP 脚本将返回一个值列表,然后 javascript 可以使用该列表来填充第二个组合框。

您可能会发现使用 JQuery 之类的工具来简化 javascript 脚本部分会更容易。

Yes, it is possible using javascript and PHP. Your javascript would use AJAX to query a PHP script giving the PHP script the value of the first combo box. The PHP script would then return a list of values that the javascript can then use to populate the second combo box.

You might find it easier to use something like JQuery to make the javascript scripting part easier.

温柔女人霸气范 2024-11-24 22:59:47

假设您的表如下所示,

mysql> select * from FRUITS;
+---------+-----------+
| fruitid | fruitname |
+---------+-----------+
|       1 | Orange    |
|       2 | Apple     |
|       3 | Pear      |
|       4 | Banana    |
+---------+-----------+
4 rows in set (0.02 sec)

那么您的代码应如下所示:

<?php

 $conn = mysql_connect('localhost','yourUsernameHere','yourPasswordHere');

 mysql_select_db('testdb',$conn);

 $query = "select fruitid,fruitname from FRUITS order by fruitname";

 $result = mysql_query($query,$conn);

 $selectbox='<select name=\'fruit\'>';

 while ($row = mysql_fetch_assoc($result)) {
     $selectbox.='<option value=\"' . $row['fruitid'] . '\">' . $row['fruitname'] . '</option>';
 }

 $selectbox.='</select>';

 mysql_free_result($result);

 echo $selectbox;

?>

Suppose your table looked like this

mysql> select * from FRUITS;
+---------+-----------+
| fruitid | fruitname |
+---------+-----------+
|       1 | Orange    |
|       2 | Apple     |
|       3 | Pear      |
|       4 | Banana    |
+---------+-----------+
4 rows in set (0.02 sec)

Your code should then look something like this:

<?php

 $conn = mysql_connect('localhost','yourUsernameHere','yourPasswordHere');

 mysql_select_db('testdb',$conn);

 $query = "select fruitid,fruitname from FRUITS order by fruitname";

 $result = mysql_query($query,$conn);

 $selectbox='<select name=\'fruit\'>';

 while ($row = mysql_fetch_assoc($result)) {
     $selectbox.='<option value=\"' . $row['fruitid'] . '\">' . $row['fruitname'] . '</option>';
 }

 $selectbox.='</select>';

 mysql_free_result($result);

 echo $selectbox;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文