我正在尝试将参数从$ .ajax帖子传递给mysqli_query
这是我的ajax
$.ajax({
type: 'post',
url: 'edit-doctor.php',
data: "imei="+imei,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
$.each(data, function() {
$.each(this, function(k , v) {
trHTML += '<tr><td><b>'+ k.toString() + '</b></td> : <td>' + v.toString() + '</td></tr>';
})
})
$("#target_table_id").append(trHTML);
}
});
这是我的php
<?php
include("connect.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$src1= $_POST["imei"]; //.. getting variable in src1
$sql = "select * from tbl_beacons where imei = '".$src1."' ";
//$sql = "select * from tbl_beacons where imei = '".$_POST['imei']."' ";
// also tried this
$result = $conn->query($sql);
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
$conn->close();
问题是我能够获取参数但无法传递查询并获得结果。如果我发送硬核值,那么查询工作正常
我可以修改以获取参数值并发送它
编辑:
我也想出并修改了ajax,这也有效
$.ajax({
type: "POST",
url: "edit-doctor.php",
data: {imei:imei},
success: function (data) {
$.each(JSON.parse(data), function() {
$.each(this, function(k , v) {
trHTML += '<tr><td><b>'+ k.toString() + '</b></td> : <td>' + v.toString() + '</td></tr>';
})
})
$("#target_table_id").append(trHTML);
}
});
答案 0 :(得分:1)
data: {imei:imei},
contentType: "application/json; charset=utf-8", dataType: "json",
更好:
$.post('edit-doctor.php', {"imei":imei},function (data) {
var trHTML=[];
$.each(data, function() {
$.each(this, function(k , v) {
trHTML.push('<tr><td><b>'+ k + '</b></td> : <td>' + v + '</td></tr>');
})
})
$("#target_table_id").append(trHTML.join(''));
});