0

我正在尝试根据 PHP 结果将不同的错误消息发送回 HTML DIV。我已经在这里和其他地方进行了搜索,但无法找到似乎适合我的情况的答案......希望其他人可以帮助我解决这个问题。

我的 HTML 页面包含一个表单,要求用户输入预订 ID #(唯一),一旦用户执行此操作并按下“搜索预订”,就会调用 Jquery 脚本以通过 AJAX 将 ID 发送到 PHP 脚本。返回数据,然后使用名为“填充”的 jquery 插件将其填充到表单中。

这是脚本:

<script type="text/javascript">
$(document).ready(function(){
    resetForms('reservation');
    $('#form-reservation').submit(function(event){ 
        event.preventDefault();  //the page will no longer refresh on form submit.
    var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check.
        $.ajax({ 
            url: 'inc/searchres.php', 
            type: 'POST',
            data: 'resid='+resCheck, 
            success: function(data){  //data is all the info being returned from the php file 
                resetForms('reservation');  //clear forms
                $('#reservation-id').val(resCheck);  //add res ID back into text box
                var jsonData = $.parseJSON(data);  //parse returned JSON data so we can use it like data.name, data.whatever 
                $("#res-message").html('<a>Reservation ID Located, Information is displayed below</a>');
                $('#json-reservation').populate({personal_first_name:jsonData['element_1_1'],personal_last_name:jsonData['element_1_2'],personal_phone_1:jsonData['element_7'],personal_email:jsonData['element_2'],reservation_status:jsonData['ADD THIS CELL'],reservation_date:jsonData['element_3'],reservation_time:jsonData['element_4'],reservation_party:jsonData['element_5'],reservation_special_request:jsonData['element_6'],reservation_using_coupon:jsonData['element_9'],reservation_coupon_code:jsonData['element_10'],reservation_status:jsonData['element_11']});
            },
            error: function(){
                $("#res-message").html('<a>There was an error with your request</a>');
            }
        });
    });
});
</script>

这是从以下形式调用的(其中还包括我想在其中填充我的错误消息的 DIV,res-message):

<form name="form_reservation" id="form-reservation">
                    <div style="padding:10px 20px 10px 10px;">
                    <label for="reservation-id">Reservation ID</label>
                        <input name="reservation_id" id="reservation-id" class="reservationid" style="width:120px !important"/>
                        <div class="res_message" id="res-message">&nbsp;</div>
                        <input type="submit" class="button" value="Search Reservation" style="width:150px !important; margin:10px 0px 0px 5px;"/>
                        <input type="button" class="button" value="Clear" style="width:150px !important; margin:10px 0px 0px 5px;" onclick="resetForms('reservation')" />
                    </div>
                </form>

填充的表格对于这个问题应该不重要。最后 PHP 看起来像这样:

<?php
    include('config-searchres.php');

    $term = $_POST['resid'];
    $sql = mysql_query("SELECT * FROM ap_form_8 WHERE id = '$term'"); //select first name (element_1_1) from form #8
    //$sql = mysql_query("SELECT * FROM ap_form_8 WHERE element_12 = '$term'");  //real selector will look for unique number that has not been added to table yet

    if ($row = mysql_fetch_array($sql)){  //if reservation number exists
        if ($row['element_11'] != 'Cancelled'){  //if reservation has not already been cancelled
            if (strtotime($row['element_3']) >= strtotime(date("Y-m-d"))){  //if reservation has not already passed date
                echo json_encode($row);
            }
            else  //Reservation already passed (old reservation)
            {
                echo $error = "Passed"; 
                //echo 'passed';
            }
        }
        else  //Reservation already cancelled
        {
            echo 'cancelled';
        }
    }
    else  //Reservation not found
    {
        echo 'not found';
    }
    mysql_close();
?>

我对 PHP 几乎一无所知,但我知道这个脚本似乎适用于填充我的表......只是在其他情况下不显示错误。3 个不同的错误,“已通过”、“已取消”和“未找到”需要在各自的情况下回传。有人有想法吗?

4

2 回答 2

0

快速而肮脏的解决方案是测试返回的结果,看它是字符串还是数组。在您的 javascript 上:

                (...)
                var jsonData = $.parseJSON(data);  //parse returned JSON data so we can use it like data.name, data.whatever
                if (typeof(jsonData)=='object'&&(jsonData instanceof Array)) {
                    // jsonData is an array, populate success div
                    $("#res-message").html('<a>Reservation ID Located, Information is displayed below</a>');
                    $('#json-reservation').populate({personal_first_name:jsonData['element_1_1'],personal_last_name:jsonData['element_1_2'],personal_phone_1:jsonData['element_7'],personal_email:jsonData['element_2'],reservation_status:jsonData['ADD THIS CELL'],reservation_date:jsonData['element_3'],reservation_time:jsonData['element_4'],reservation_party:jsonData['element_5'],reservation_special_request:jsonData['element_6'],reservation_using_coupon:jsonData['element_9'],reservation_coupon_code:jsonData['element_10'],reservation_status:jsonData['element_11']});

                } else {
                    // jsonData is a simple string, so PHP returned an error. populate error div
                    $("#res-message").html('<a>There was an error with your request</a>');
                }
                (...)

当然,在处理错误时,如果需要,您可以打开 jsonData 内容以提供特定的错误消息。

于 2012-04-25T02:30:06.240 回答
0

似乎问题在于来自 PHP 代码的不同错误消息不是作为 JSON 字符串发送的。所以当使用 'jsonData = $.parseJSON(data);' 它给出了例外。也许您可以尝试使用以下代码。

    <script type="text/javascript">
    $(document).ready(function(){
     resetForms('reservation');
     $('#form-reservation').submit(function(event){ 
     event.preventDefault();  //the page will no longer refresh on form submit.
     var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check.
     $.ajax({ 
        url: 'inc/searchres.php', 
        type: 'POST',
        data: 'resid='+resCheck, 
        success: function(data){  //data is all the info being returned from the php file 
            resetForms('reservation');  //clear forms
            $('#reservation-id').val(resCheck);  //add res ID back into text box
            try 
            {
             var jsonData = $.parseJSON(data);  //parse returned JSON data so we can use it like data.name, data.whatever 
             $("#res-message").html('<a>Reservation ID Located, Information is displayed below</a>');
             $('#json-reservation').populate({personal_first_name:jsonData['element_1_1'],personal_last_name:jsonData['element_1_2'],personal_phone_1:jsonData['element_7'],personal_email:jsonData['element_2'],reservation_status:jsonData['ADD THIS CELL'],reservation_date:jsonData['element_3'],reservation_time:jsonData['element_4'],reservation_party:jsonData['element_5'],reservation_special_request:jsonData['element_6'],reservation_using_coupon:jsonData['element_9'],reservation_coupon_code:jsonData['element_10'],reservation_status:jsonData['element_11']});
            }
            catch 
            {
              $("#res-message").html('<a>There was an error with your request</a>');
              $('#json-reservation').populate({status:data});
            }
          }
        });
      });
     });
     </script>

我没有测试过这段代码,但至少逻辑应该可以工作。

于 2012-04-25T12:34:01.257 回答