php ajax 两个选择框

发布于 2024-12-19 14:31:51 字数 1424 浏览 1 评论 0原文

我对这个 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 技术交流群。

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

发布评论

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

评论(2

仄言 2024-12-26 14:31:51

将此 javascript 添加到您的当前页面:

<script>
$(function () {

  $("#first").change(function () {
    $("#second").load('second_options.php?year=' + $(this).val());
  });

});
</script>

并在“second_options.php”中返回此内容:

  <option value="1">a</option>
  <option value="2">b</option>

Add this javascript to your current page:

<script>
$(function () {

  $("#first").change(function () {
    $("#second").load('second_options.php?year=' + $(this).val());
  });

});
</script>

And return this content in your "second_options.php":

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