Php 从数据库获取表单中选择的选项
我正在用PHP编程服务器端,但遇到了问题 我想使用表单的选择标记创建一组输入字段。 select的选项应从我的数据库中获取,第一个输入中选择的选项将决定第二个输入中的选项Php 从数据库获取表单中选择的选项,php,html,mysql,Php,Html,Mysql,我正在用PHP编程服务器端,但遇到了问题 我想使用表单的选择标记创建一组输入字段。 select的选项应从我的数据库中获取,第一个输入中选择的选项将决定第二个输入中的选项 例如,这两个字段是国家和州。首先,用户选择他们的国家名称,这将决定出现在状态输入字段中的状态列表。我希望列表在用户更改国家/地区时动态更改。您使用的是Laravel推荐的php框架还是Codeigniter不确定这是否还在开发中 如果是这样,您可以构建一个二维php国家数组,每个国家都有一个子数组 这可以直接放在渲染页面视图中
例如,这两个字段是国家和州。首先,用户选择他们的国家名称,这将决定出现在状态输入字段中的状态列表。我希望列表在用户更改国家/地区时动态更改。您使用的是Laravel推荐的php框架还是Codeigniter不确定这是否还在开发中 如果是这样,您可以构建一个二维php国家数组,每个国家都有一个子数组 这可以直接放在渲染页面视图中。 使用类似
var countries_list =<?php echo json_encode($countries_array); ?>;
将直接将其注入javascript数组,当国家选择更改时,您可以使用该数组填充状态选择
或者使用ajax,但这样会更慢,对服务器的影响更大。您可以在PHP中完成,每次提交一个select
例如:
<form action="selectState.php" method="post">
<select name="country">
<option value="countryName">countryName</option>
...
</select>
</form>
您最不需要的是它的“page.php”,它只需查询DB注意:这是伪代码:
<?php
// Query the DB for the states with country = $_POST['country']
while(results){
echo '<option value="results[$i]">results[$i]</option>';
}
?>
<form action="selectState.php" method="post">
<select name="country" onchange="ajaxFunction(this.value)">
<option value="countryName">countryName</option>
...
</select>
<select name="state" id="stateList">
<!-- here we'll put the states we want -->
</select>
</form>
function ajaxFunction(val){
$.ajax({
type: 'post', // choose the method
url: 'page.php', // choose the 'action' page
data: {
country:val // send the data
},
success: function (response) {
// this tell the browser what to do with the response of 'page.php'
// in this case we are telling to put everything we get to the HTML element with stateList id
document.getElementById("stateList").innerHTML=response;
}
});
}
<?php
// Query the DB for the states with country = $_POST['country']
while(results){
echo '<option value="results[$i]">results[$i]</option>';
}
?>