【发布时间】:2018-06-25 22:37:56
【问题描述】:
我有 3 列
- 国家(id,country_name)
- 州(id、country_id、state_name)
- files_dev (id,country_name,state_name)
国家和州我正在使用 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 提交到数据库。
【问题讨论】:
-
显示您的查询。我们也帮不了你。
-
if (isset($_POST['save'])){ $gp = $_POST['country']; $query = mysql_query("select * from files_dev where file = '$file' ")or die(mysql_error()); $count = mysql_num_rows($query); if ($count > 0){ ?> <script> alert('Files already Exist'); window.location = "upload_files_dev.php"; </script> <?php }else{ if(move_uploaded_file($file_loc,$folder.$final_file)){ mysql_query("insert into files_dev (id,gp,village,year,rec_type,file,type,size) values('','$gp','$village','$year','$rec_type','$file','$file_type','$file_size')")or die(mysql_error()); -
对不起,如果你能理解我的查询,我也在插入其他记录。
-
我认为这不是您在此处粘贴的相关代码。
-
嗨 Ankit 兄弟,我已经粘贴了所有需要的代码,请帮助看看我们是否可以显示名称而不是将 id 传递给数据库