这是我调用ajax函数的js。
function gettaluka(id)
{
if(id){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:{'id':id},
success:function(data){ //console.log(html); return;
$('#talukaplace').html(data);
storetaluka();
}
});
}else{
$('#talukaplace').html('<option value="">Select Taluka</option>');
}
}
在此,我进行了2个下拉菜单。基于对第一个下拉列表的选择,即赋值,我正在第二个下拉列表中从db中获取数据。
如果我选择选项值1,则必须将其各自的数据存储在数据库表中,例如assign,其col字段为“ cost_assignment”;当我选择选项值2时,必须将其各自的数据插入到同一表中但在不同的列字段为“ proj_assignment”。
<td align="left">
Assignment
<span class="redtext">
*
</span>
</td>
<td>
<div class="">
<div class="form-group">
<!-- <input type="text" class="form-control" name="distplace1" id="distplace1" placeholder="District Preference 1*" > -->
<select class="form-control" id="distplace1" name="distplace1" onchange="gettaluka(this.value);" required="">
<option value="0">
Select District
</option>
<option value="1">
Cost Chargeable
</option>
<option value="2">
Project Name
</option>
<option value="3">
Other Recovery
</option>
</select>
</div>
</div>
</td>
<td>
<div class="">
<div class="form-group">
<select class="form-control" id="talukaplace" maxlength="10" name="talukaplace" required="">
<option maxlength="10" value="0">
Select Taluka
</option>
</select>
</div>
</div>
</td>
ajaxData.php
$dist_id = $_POST["id"];
if ($dist_id == '1') {
?>
<html>
<?php
$query = "select cost_name from hris_cost_centers";
$results = mysqli_query($conn, $query);
// $rowCount = $query->num_rows;
if (mysqli_num_rows($results) > 0) {
echo '<option value="" maxlength="100">Select Taluka</option>';
while ($row = mysqli_fetch_assoc($results)) {
echo '<option value="' . $row['cost_name'] . '">' . $row['cost_name'] . '</option>';
}
} else {
echo '<option value="">Select Taluka</option>';
}
}
;
elseif ($dist_id == '2') {
$query = "select bta_proj_name from bta_project";
$results = mysqli_query($conn, $query);
if (mysqli_num_rows($results) > 0) {
echo '<option value="">Select Project</option>';
while ($row = mysqli_fetch_assoc($results)) {
echo '<option value="' . $row['bta_proj_name'] . '">' . $row['bta_proj_name'] . '</option>';
}
} else {
}
}
根据要插入到数据库中的下拉列表值的选择,我有2个下拉列表。我可以插入它,但值会插入1列中