我有一个表格,用户必须输入他们的预订ID和姓氏。如果数据库中这两个值匹配,那么我需要从数据库中返回相应的值。
看起来即使输入了错误的预订ID和名称,也将其视为成功。页面将重新加载并显示foobar。
这是我的代码
test06.php
<?php
$conn = mysqli_connect("","","","");
$reservation_id=$_POST['reservation_id'];
$guest_last_name=$_POST['guest_last_name'];
$stmt = $conn->prepare("SELECT reservation_id, guest_last_name, guest_full_name, unit_number, floor, key_sa
FROM reservations2
INNER JOIN guest ON (reservations2.reservation_id=guest.reservation_idg)
INNER JOIN unit USING (unit_id)
WHERE reservation_id=?
AND guest_last_name=?");
$stmt->bind_param("ss", $reservation_id, $guest_last_name);
$stmt->execute();
$stmt->bind_result($reservation_id, $guest_last_name,
$guest_full_name, $unit_number,
$floor, $key_sa);
if ($stmt->errno) {
die("Query failed to execute: " . $stmt->error);
}
if ($stmt->fetch()) {
echo json_encode(array("reservation_id" => $reservation_id,
"guest_last_name" => $guest_last_name,
"guest_full_name" => $guest_full_name,
"unit_number" => $unit_number,
"floor" => $floor,
"key_sa" => $key_sa));
} else {
$error="Not matching record";
echo json_encode($error);
}
$stmt->close();
?>
内部HTML页面
<p id='guest_full_name'></p>
<p id='unit_number'></p>
<p id='floor'></p>
<p id='error'></p>
<script>
function validateReservation(){
var reservation_id = document.getElementById("reservation_id").value;
var guest_last_name = document.getElementById("guest_last_name").value;
$.ajax({
type: 'POST',
url: 'test06.php',
// dataType: 'json',
data: {
'reservation_id': reservation_id,
'guest_last_name' : guest_last_name
},
success: function(json) {
var json = JSON.parse(json);
console.log(json);
$('#guest_full_name').html(json.guest_full_name);
$('#unit_number').html(json.unit_number);
$('#floor').html(json.floor);
$('#key_sa').html(json.key_sa);
},
error: function(err) {
console.log(err);
$("#error").html("Error!");
}
});
}
答案 0 :(得分:0)
您必须发送状态码。回显JSON只会给您200响应。
header('Content-Type: application/json');
http_response_code(500);
echo $json;
exit;