2

有人可以帮忙吗?这是我的 jQuery。场景是这样的,我有一个表格,其中每个按钮都有一个按钮<td>。如果单击该按钮,将出现一个模态窗口,上面有一个确认按钮,是或否。如果用户单击是 ( #confirm_cancel),这将保存在数据库中,然后显示一个警告框,告诉用户更新成功。问题是这样的,它正确执行了我的 SQL 并保存到数据库中,但我在错误函数上收到一个警告框,上面写着[object object].

这是我的PHP:

<?php
include("php/openDB.php"); 

echo '<table border="2" id="tableGuestList">';
echo '<tr>';
    echo '<td>Guest ID</td>';
    echo '<td>Room No:</td>';
    echo '<td>First Name</td>';
    echo '<td>Last Name</td>';
    echo '<td>Checkin</td> ';
    echo '<td>checkout</td>';
    echo '<td>Status</td>';
    echo '</tr>';

    $result = mysql_query("SELECT gi.guest_id, gi.fname, gi.lname, bk.checkin, bk.checkout, bk.transacstatus, bk.reserved_id "
    ."FROM tbl_guestinfo gi, tbl_bookings bk "
    ."where gi.guest_id = bk.guest_id "
    ."AND bk.transacstatus = 'booked'");

    while ($rows = mysql_fetch_array($result, MYSQL_NUM)) {
        $guest_id = $rows[0];

        $fname = $rows[1];
        $lname = $rows[2];
        $checkin = $rows[3];
        $checkout = $rows[4];
        $transacstatus = $rows[5];
        $reservedid = $rows[6];


        echo '<tr id="'.$rows[0].'" class="bookedClass">';
        echo '<td>'.$guest_id.'</td>';
        echo '<td> room number kaara</td>';
        echo '<td> '.$fname.'</td>';
        echo '<td>'.$lname.'</td>';
        echo '<td> '.$checkin.'</td>';
        echo '<td>'.$checkout.'</td>';
        echo '<td> '.$transacstatus.'</td>';

        echo '<td style="display:none"> '.$reservedid.'</td>';

        echo '<td> <input type = "submit" value = "Cancel Booking" name ="cancel_booking" /></td>';

        echo '<td> <input type = "submit" value = "Check in" name ="check_in" /></td>';

        //echo '<td> <input type = "submit" value = "Check Out" name ="Check_Out" /></td>';
        echo '</tr>';
    }

    mysql_free_result($result);

    echo '</table>';

    include("php/closeDB.php");
?>

这是我的jQuery

 $(document).ready(function(){
    var selGuest;
    $('#tableGuestList .bookedClass').click(function(){
        selGuest = $(this).find("td").eq(7).text();
        selGuest = parseInt(selGuest)
        $('#confirmDialog').fadeIn('slow');
    });

    $('#confirm_cancel').click(function() {
        var canceldata_json = {
            'selGuest': selGuest,
        };
        $.ajax({
            type: "POST",
            data: canceldata_json,
            url: "./php/cancelBooking.php",
            success: function(msg) {
                alert("guest information updated")
                $('#confirmDialog').fadeOut('slow');
            },
            error: function(msg){
                alert(msg)
            }
        });
    });

    $('#cancel_cancel').click(function() {
        $('#confirmDialog').fadeOut('slow');
    });
});

这是我的 PHP 代码被 AJAX 调用

<?php

// get data
$selGuest = $_POST["selGuest"];

include("openDB.php");

$insertintoCanceled = "insert into tbl_canceled "
        ."reserved_id, `guest_id`,`checkin`, `checkout`, `type_id`, `numAdults`, `numChildren`, `transacstatus`, `amountDue`"
        ."("
        ."SELECT * FROM `tbl_bookings` where `reserved_id` =" .$selGuest
        .")";
if(!mysql_query($insertintoCanceled, $con))//if it fails
{
    die('Error: ' . mysql_error() . "\n");//show the mysql error
}
include("closeDB.php");
?>
4

3 回答 3

0

这可能是由alert(msg). msg可能是一个对象,而您想要的实际数据是它的属性值之一。尝试在错误函数中的 Chrome 开发工具或 Firebug 中设置断点并检查msg包含的值。它可能就像使用一样简单 alert(msg.d)

于 2012-09-20T07:08:52.283 回答
0

您的“error:function(msg)”回调函数没有使用正确的参数。

我会建议:

error: function(jqXHR, textStatus, errorThrown)
{
    console.log(jqXHR, textStatus, errorThrown); //Check with Chrome or FF to see all these objects and decide what you want to display
    alert('Error! '+textStatus);
}

http://api.jquery.com/jQuery.ajax/

此外,检查您的 PHP 错误日志,看看它是否符合您的预期。

于 2012-09-20T07:10:57.260 回答
0

假设您只想要“上传成功”消息。

我认为最好将 dataType 用作 JSON(服务器以 JSON 格式响应)。在你的 AJAX 中试试这个:

$.ajax({
    type: "POST",
    data: canceldata_json,
    dataType:'json',
    url: "./php/cancelBooking.php",
    success: function(msg) {
        alert(msg)
        $('#confirmDialog').fadeOut('slow');
    },
    error:function(msg){
        alert(msg)
    }
});

和你的 php 函数

<?php

// get data
$selGuest = $_POST["selGuest"];

include("openDB.php");

$insertintoCanceled = "insert into tbl_canceled "
    ."reserved_id, `guest_id`,`checkin`, `checkout`, `type_id`, `numAdults`,        `numChildren`, `transacstatus`, `amountDue`"
."("
."SELECT * FROM `tbl_bookings` where `reserved_id` =" .$selGuest
.")";
if(!mysql_query($insertintoCanceled, $con))//if it fails
{
echo json_encode(array('msg'=>'Error')) //error msg goes here
die('Error: ' . mysql_error() . "\n");//show the mysql error

}
echo json_encode(array('msg'=>'Successfully updated')) //success msg goes here
include("closeDB.php");
?>
于 2012-09-20T07:16:36.913 回答