2012-09-20 24 views
2

有人可以幫忙嗎?這是我的jQuery。場景是這樣的,我有一張桌子,每個按鈕上都有一個按鈕,如果你點擊那個按鈕,一個模式窗口會出現一個確認按鈕,並且是或不是。如果用戶點擊是(#confirm_cancel),這將保存在數據庫中,然後顯示一個警告框,其中它告訴用戶更新成功。問題是這樣,它正確執行我的SQL並保存到數據庫,但我得到一個錯誤函數的警告框,說[object object]對象對象在運行SQL後通過AJAX/jQuery在警告框上返回

這是我的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"); 
?> 
+0

但是,當我做到這一點,把它轉移到另一個點擊功能,它的工作。 $('#confirm_cancel')和$('#cancel_cancel')按鈕位於我在點擊$('#tableGuestList .bookedClass') – propaganja

+0

$('#tableGuestList .bookedClass')後生成的模式窗口中。函數(){風險canceldata_json = { 'selGuest':selGuest, }; $就({ 類型: 「POST」, 數據:canceldata_json, URL: 「./php/cancelBooking.php」, 成功:函數(MSG){ 警報( 「更新顧客信息」) $( '#confirmDialog')淡出( '慢'); }, 錯誤:函數(MSG){ 警報(MSG) } ) }); – propaganja

+0

這是我從螢火蟲中得到的。對象{readyState = 0,status = 0,statustext =「erro」}錯誤(一個空字符串) 實際上它只是在我的屏幕上閃爍。彈出提醒然後我甚至不點擊確定按鈕,它關閉...任何幫助球員 – propaganja

回答

0

這可能由以下原因引起所謂的alert(msg)msg可能是一個對象,並且您想要的實際數據是其屬性值之一。嘗試在錯誤功能中設置Chrome開發工具或Firebug中的斷點,並檢查msg包含的值。這可能與使用alert(msg.d)代替一樣簡單。

+0

謝謝。我確實嘗試了斷點,但我不知道我想知道什麼。我的意思是我需要看或如何..請幫助。謝謝 – propaganja

0

您的「錯誤:函數(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錯誤日誌,看它是否做你想讓它做什麼。

+0

感謝您的快速回復。我確實嘗試過你的解決方案,只是改變了錯誤。問題是我已經成功更新了我的數據庫中的表格,但是爲什麼它仍然彈出。我相信它應該在成功更新之後才能取得成功。順便說一下,我測試了我的sql語句並且工作正常。請問你能幫我嗎 – propaganja

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

和UR 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"); 
?> 
+0

謝謝。我確實嘗試了這一點,但仍然得到了應該成功的錯誤,因爲我成功更新了數據庫。 – propaganja