php ajax 两个选择框
我对这个 jquery ajax 很陌生。我想知道的是如何根据第一个选择框的选择填充第二个选择框的内容。这里说的是我的 HTML:
<form method="post" action="tosomewhere.php">
<table>
<tbody>
<tr>
<td>
<select id="first" name="year">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="second" name="section">
</select>
</td>
</tr>
</tbody>
</table>
</form>
jscript:
<script type="text/javascript">
$(function () {
$("#first").change(function () {
$("#second").load('second_option.php?year=', {first: $(this).val()});
});
});
</script>
PHP 文件:
<?php
require '../../config/dbconfig.php';
$year = $_GET['year'];
$sql = "SELECT * FROM section_tb WHERE year = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array($year));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$second_option = "";
foreach($result as $key => $value)
$second_option .= "<option value='".$value['section_name']."'>".$value['section_name']."</option>";
echo $second_option;
?>
我的第二个选择框的内容将从第一个选择框的 WHERE 值的 mysql 结果中填充。我可以构建 PHP 文件,我只是对如何返回并在选择上填充它感到困惑。
有教程或示例的线索吗?太感谢了。
编辑:添加了 jquery 脚本,但仍然没有运气。我尝试 var_dump($second_option) 它有内容。
I am quite new to this jquery ajax. What I want to know is how can I populate the content of my second select box based on the selection from the first select box. Say here is my HTML:
<form method="post" action="tosomewhere.php">
<table>
<tbody>
<tr>
<td>
<select id="first" name="year">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="second" name="section">
</select>
</td>
</tr>
</tbody>
</table>
</form>
The jscript:
<script type="text/javascript">
$(function () {
$("#first").change(function () {
$("#second").load('second_option.php?year=', {first: $(this).val()});
});
});
</script>
The PHP file:
<?php
require '../../config/dbconfig.php';
$year = $_GET['year'];
$sql = "SELECT * FROM section_tb WHERE year = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array($year));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$second_option = "";
foreach($result as $key => $value)
$second_option .= "<option value='".$value['section_name']."'>".$value['section_name']."</option>";
echo $second_option;
?>
The content of my second select box will be populated from a mysql results of WHERE value of first select box. I can build the PHP file, I'm just confuse on how to return and populate it on the select.
Any leads to tutorials or samples? Thank you so much.
EDIT: Added the jquery script, but still no luck. I tried to var_dump($second_option) it has contents.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此 javascript 添加到您的当前页面:
并在“second_options.php”中返回此内容:
Add this javascript to your current page:
And return this content in your "second_options.php":
这里有一些教程
www.huanix.com/files/dependent_select/dependent_select.php
http://bytes.com/topic/php/answers/708593-dependent-dropdown-list-mysql
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
here are some tutorials
www.huanix.com/files/dependent_select/dependent_select.php
http://bytes.com/topic/php/answers/708593-dependent-dropdown-list-mysql
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php