我有3列
Country和State我使用onchange事件处理程序来选择元素,然后每次用户选择显示相关州名的国家时,都会向我的PHP脚本发出一个AJAX请求。
问题在于POST SUBMIT我想插入国家名称而不是其值id
这是我正在使用的代码
<?php
if ($country_result->num_rows > 0) {
// output data of each row
while($row = $country_result->fetch_assoc()) {
?>
<option value="<?php echo $row["id"]; ?>">
<?php echo $row["country_name"]; ?></option><?php
我尝试将$ row [“id”]更改为$ row [“country_name”],此处的任何帮助都将受到高度赞赏
的index.php
<form class="form-horizontal" method="post" id="signin" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="inputPassword" placeholder="Select Gram Panchayat" ><strong>Select Gram Panchayat</strong></label>
<div class="controls">
<?php
require_once('../admin/lib/db.php');
$country_result = $conn->query('select * from countries');
?>
<select name="country" class="chzn-select" id="countries-list" required>
<?php
if ($country_result->num_rows > 0) {
// output data of each row
while($row = $country_result->fetch_assoc()) {
?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["country_name"];?></option>
<?php
echo "<option value='". $row['id']."'>".$row['country_name'] .'</option>';
}
}
?>
</select>
</div>
</div>
<div class="control-group">
<label for="inputPassword" class="control-label" placeholder="Select Village" ><strong><strong>Select Village</strong></strong></label>
<div class="controls">
<select name="state" id="states-list" required>
</select>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$('#countries-list').on('change', function(){
var country_id = this.value;
$.ajax({
type: "POST",
url: "village_dev.php",
data:'country_id='+country_id,
success: function(result){
$("#states-list").html(result);
}
});
});
</script>
village_dev.php
<?php
require_once('../admin/lib/db.php');
error_reporting(0);
$country_id = mysql_real_escape_string($_POST['country_id']);
if($country_id!='')
{
$states_result = $conn->query('select * from states where country_id='.$country_id.'');
$options = "<option value=''>Select Village</option>";
while($row = $states_result->fetch_assoc()) {
$options .= "<option value='".$row['state']."'>".$row['state']."</option>";
}
echo $options;
}
?>
如果我在这里遗漏了任何内容,请告诉我,我想将值名称而不是id提交给数据库。
答案 0 :(得分:0)
这里的问题是你想获得一些信息在之外输入值
这就是为什么你会获得id而不是州名!
<form class="form-horizontal" method="post" id="signin" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="inputPassword" placeholder="Select Gram Panchayat" ><strong>Select Gram Panchayat</strong></label>
<div class="controls">
<?php
require_once('../admin/lib/db.php');
$country_result = $conn->query('select * from countries');
?>
<select name="country" class="chzn-select" id="countries-list" required>
<?php
if ($country_result->num_rows > 0) {
// output data of each row
while($row = $country_result->fetch_assoc()) {
?>
<option value="<?php echo $row["country_name"]; ?>"><?php echo $row["country_name"];?></option>
<?php
echo "<option value='". $row['country_name']."'>".$row['country_name'] .'</option>';
}
}
?>
</select>
</div>
</div>
<div class="control-group">
<label for="inputPassword" class="control-label" placeholder="Select Village" ><strong><strong>Select Village</strong></strong></label>
<div class="controls">
<select name="state" id="states-list" required>
</select>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$('#countries-list').on('change', function(){
var country_id = this.value;
$.ajax({
type: "POST",
url: "village_dev.php",
data:'country_id='+country_id,
success: function(result){
$("#states-list").html(result);
}
});
});
</script>
所以现在你将能够将你需要的内容发布到你的数据库中,输入的值将是状态的名称!
干杯
答案 1 :(得分:0)
尝试html5的data-
属性:
将您的国家/地区下拉代码更改为:
<?php
require_once('../admin/lib/db.php');
$country_result = $conn->query('select * from countries');
?>
<select name="country" class="chzn-select" id="countries-list" required>
<?php
if ($country_result->num_rows > 0) {
// output data of each row
while($row = $country_result->fetch_assoc()) {
echo '<option value="'.$row["country_name"].'" data-countryID="'.$row["id"].'">'.$row["country_name"].'</option>';
}
}
?>
</select>
并使用以下内容更改您的ajax脚本:
<script>
$('#countries-list').on('change', function(){
var country_id = $('#countries-list option:selected').data( 'countryID' );
$.ajax({
type: "POST",
url: "village_dev.php",
data:'country_id='+country_id,
success: function(result){
$("#states-list").html(result);
}
});
});
</script>