段落内的Ajax PHP打印错误

时间:2018-07-09 12:12:34

标签: php ajax

我有一个表格,用户必须输入他们的预订ID和姓氏。如果数据库中这两个值匹配,那么我需要从数据库中返回相应的值。

如果保留名或姓氏不匹配,我试图在段落内打印错误。我确实在控制台中看到了错误,但在页面中却没有看到。

看起来即使输入了错误的预订ID和名称,也将其视为成功。即使错误,页面也会重新加载。

这是我的代码

<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!");
            } 
    }); 
}

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();

?>

3 个答案:

答案 0 :(得分:1)

如果您尝试使用error id属性查看元素内部的错误,请使用相同的err变量而不是错误一词(除非我误解了)。

这是您的原始代码:

error: function(err) {
    console.log(err);
    $("#error").html("Error!"); //<-- Replace this...
}

尝试此更新的代码:

error: function(err) {
    console.log(err);
    $('#error').text(err); //<-- ...with this.
}

答案 1 :(得分:0)

如果在控制台中看到变量json的数据,那么我认为问题是浏览器在进行AJAX调用时检测到的MIME类型。

也就是说,当您调用网页(php或html)时,响应标题中的服务器告诉浏览器它返回的信息是text/html

因此,在PHP中,您可以告诉客户端(接收数据的浏览器)其类型为application/json

在PHP代码的开头添加:

header('Content-Type: application/json');

test06.php

<?php
header('Content-Type: application/json'); // <-- MIME JSON
$conn = mysqli_connect("","","","");
$reservation_id=$_POST['reservation_id'];
$guest_last_name=$_POST['guest_last_name'];
$stmt = $conn->prepare(QUERY);
$stmt->bind_param("ss", $reservation_id, $guest_last_name);
$stmt->execute();
$stmt->bind_result(VARS);
if ($stmt->errno) {
    die("Query failed to execute: " . $stmt->error);
}
if ($stmt->fetch()) {
    echo json_encode(ARRAY);
} else {
    $error="Not matching record";
    echo json_encode($error);
}
$stmt->close();

请记住不要在空白之前 <?php

答案 2 :(得分:0)

没有服务器错误,因此它没有打印出来。我必须发送状态码才能正常工作

header('Content-Type: application/json');
http_response_code(500); 
echo $json;
exit;