Php 在第一次选择时更改第二次选择的值
很抱歉,我没有将我的尝试包括在这篇文章中,我对jquery毫无用处,因此需要一些建议 我想根据第一个选择器的结果更改第二个选择器的值 我有一个建设者和地区的数据库,标题为builder\u name和builder\u region。列表最后看起来像这样Php 在第一次选择时更改第二次选择的值,php,jquery,mysql,select,Php,Jquery,Mysql,Select,很抱歉,我没有将我的尝试包括在这篇文章中,我对jquery毫无用处,因此需要一些建议 我想根据第一个选择器的结果更改第二个选择器的值 我有一个建设者和地区的数据库,标题为builder\u name和builder\u region。列表最后看起来像这样 builder_1 region_1 builder_1 region_2 builder_2 region_1 builder_3 region_1 builder_3 region_2 builder_3 region_3 你明白了吗
builder_1 region_1
builder_1 region_2
builder_2 region_1
builder_3 region_1
builder_3 region_2
builder_3 region_3
你明白了吗
我在我构建的表单中使用以下内容,以获取第一个选择框的构建器列表
echo '<select class= "ml-select" name="gen_builder">';
echo '<option value=" ">Select Builder</option>';
while($row = mysql_fetch_array($rsBUILDER)) {
if($linebreak !== $row['builder_name']) {
echo '<option value="'.$row['builder_name'].'">'.$row['builder_name'].'</option>';
}
else {echo "";}
$linebreak = $row['builder_name'];
}
echo '</select>';
$linebreak是为了从列表中删除重复的构建器名称,这非常有用
我想要实现的是第二个选择器,它根据在第一个选项中选择的构建器来选择可用的区域。我希望这是有意义的
第二个查询需要查看在第一个选择器中选择的构建器,并仅过滤出具有构建器名称form selector one的行中的区域
如果您需要进一步的信息,请务必说明,我不擅长解释自己。正如您所说,您没有使用jQuery或Ajax的经验,我将发布我的答案,并尽可能多地发表评论。我假设您的页面中有两个选择下拉列表 第一个包含构建器,因此它的id=builders 第二个包含区域,因此它的id=区域 据我所知,第一个选择将完全是您在问题中发布的,由PHP在服务器端生成。我只要求您对其稍作更改,使每个选项值等于构建器的数据库ID,而不是其名称,除非构建器的主键是其名称,而不是ID。这对最终用户没有影响,但对我们的jQuery解决方案很重要。第二个将是空的,因为其思想是用与第一个下拉列表中选择的构建器相关的区域动态填充它 现在让我们来看jQuery代码:
尽管有语法错误,但不能从这里测试代码,这应该可以做到。希望这些评论足够清晰,让您能够理解jQuery中的工作原理。使用jQuery/Ajax并不难做到这一点。我想你从来没有使用过它们中的任何一个?所以你的设计显示了一个建设者列表。然后,当您选择一个构建器时,它会显示它们工作的区域列表?这怎么可能有用呢?@lucasnadalutti说得对,我没有经验,也没有翻阅我正在苦苦挣扎的文档。我不需要有人为我编写代码,这要求太高了,但是一些关于在哪里查找的指针会很好。@草莓-奇怪的问题,这显然对我的项目很重要????如果是商店,那么问题就没有什么不同了,问题是如何在选择第一个选择框时影响第二个选择框,而不是讨论其中的字段。如果这听起来很粗鲁,我很抱歉,但我认为这个网站是关于编码帮助而不是对内容的批评?它还不是批评。是好奇,是困惑。商店是不同的。许多商店都是连锁店。你通常对哪家商店卖什么以及你是否喜欢在那里购物有一个清晰的概念。所以,你可以选择一家商店,然后根据你的行程选择一家最容易到达的商店。建设者不是铁链……哇,谢谢你这么多的麻烦和解释!一切正常。缺乏经验意味着在查询产生我想要的结果之前,不必对get_regions.php文件进行一点实验,但现在运行良好。谢谢你的帮助!!:不客气。很高兴它有帮助!请将我的答案标记为选中,这样就更有可能帮助其他有类似问题的人。
//Everything that goes below this first line will be ready as soon as the page is fully loaded
$(document).ready(function() {
//The following code defines an event. More precisely, we are defining that the code inside it will run every time our select with id "builders" has its value changed
$('#builders').change(function() {
//$(this) is our builders select. $(this).val() contains the selected value, which is the ID of our selected builder
var currentValue = $(this).val();
//Now, this is our Ajax command that will invoke a script called get_regions.php, which will receive the builder's ID in $_GET['builder_id'] and you can use to query your database looking for all regions related to this builder. Make sure to create an array with the returned regions. Your get_regions.php's last line should be echo json_encode($regions);
$.get("get_regions.php", {'builder_id': currentValue}, function(data) {
//Inside this function is the code that will run when we receive the response from our PHP script. It contains a JSON encoded list of regions, so first of all we need to parse this JSON
var regions = $.parseJSON(data);
//Before filling our second select dropdown with the regions, let's remove all options it already contains, if any
$('#regions').empty();
//Now, all there is left is to loop over the regions, adding each as an option of the regions dropdown. I'll do it the universal way
for (var i = 0; i < regions.length; i++) {
var regionOption = '<option value="'+regions[i]['region_name']+'">';
regionOption += regions[i]['region_name'];
regionOption += '</option>';
$('#regions').append(regionOption);
}
});
});
});