我正在尝试将变量 optionSelected 传递给另一个页面 get_usersjson.php。然后该值将用于运行 sql 查询。get_usersjson.php 返回的值将用于填充下拉菜单中的选项。但此代码似乎不起作用。
$('#tabletomodify').on('change', '.selectname',
function () {
//alert( this.parentNode.parentNode.rowIndex );
var row = ($(this).closest('tr').prop('rowIndex'));
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
$.ajax({
type: "POST",
url: "get_usersjson.php",
data: valueSelected,
function (data) {
var html = '';
var len = data.length;
for (var i = 0; i < len; i++) {
html += '<option value="' + data[i].monthId + '">' + data[i].month + '</option>';
}
$('select.Location').append(html);
}
});
}
);
下面是get_usersjson.php
<?php
$a = $_GET['a'];
//$a=1;
$connection_for_user_location= mysqli_connect('localhost', 'xxxx', 'xxxx', 'xxxx') or die ('Cannot connect to db');
$sql_query_2 = "select * from user_info";
$result_query_2=mysqli_query($connection_for_user_location,$sql_query_2) or die ("Fail");
$result_query_2->data_seek($a);
$row_query_2=$result_query_2->fetch_row();
for ($i=3;$i<=21;$i++)
{
$locality[$i]=$row_query_2[$i];
$city[$i] = $result_query_2->fetch_field_direct($i);
if(($locality[$i] != null) && ($line_card[$i] != "Empty"))
{
$data[]=$city[$i]->name.'::'.$locality[$i];
}
}
echo json_encode($data);
mysqli_close($connection_for_user_location);
?>
这是 php 页面的示例输出:-
["Select City::Select Locality","Bangalore::MG Road","Delhi::Shalimar Bagh","New Delhi::Barakhamba Road","Punjab::Ghuman Nagar"]