11

我有一个 JQGRid,它显示带有双击事件的预订,如下所示:

ondblClickRow: function(rowid)
{
    rowData = $("#bookings").getRowData(rowid);
    var brData = rowData['bookref'];
    getGridRow(brData);
},

这被传递给 getGridRow 函数:

function getGridRow(brData) {

    //$.post('bookings-dialog.php', { 'rowdata': brData } );
                 //  $("#cp-bookings-dialog").load('bookings-dialog.php').dialog({ show: "slide", hide: 'slide', height: 625, width: 733, title: 'Booking Reference: - '+ brData});

    $.ajax({
      url: 'bookings-dialog.php',
      type:'POST',
      data: {'rowdata' : brData },
      dataType: 'JSON', //this is what we expect our returned data as
    error: function(){
      alert("It failed");
      $('#cp-div-error').html('');
      $('#cp-div-error').append('<p>There was an error inserting the data, please try again later.</p>');
      $('#cp-div-error').dialog('open');
    },
    success: function(data){
        alert("IT WORKED!");
        //empty our dialog so we don't end up with duplicate content
        $('.cp-booking-info').empty();
        //we have told the browser to expect JSON back, no need to do any parsing
        //the date
        $('#cp-bookings-dialog').append('<p class="pno-margin">Booking Date: '+data.bookref+'</p>');


        //now let's manipulate our dialog and open it.
        $("#cp-bookings-dialog").dialog({
          show: { effect: 'drop', direction: "up" },
          hide: 'slide',
          height: 625,
          width: 733,
          title: 'Booking Reference: - '+ brData
        });
      }

    });

这是bookings-dialog.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
require_once('deployment.php');
require_once('bootstrp/all.inc.php');;

require_once('models/sql.php');
require_once('models/bookingdocket.php');

    $pdo = new SQL();
    $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

    try {
           $rowdata = $_POST['rowdata'];

           $query = ("SELECT * FROM tblbookings WHERE bookref = '$rowdata'");

        $stmt = $dbh->prepare($query);

        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_BOTH);

          /* BookingDocket::set_id($row['id']); */
           BookingDocket::set_bookref($row['bookref']);
          /* BookingDocket::set_bookdate($row['bookingdate']);
           BookingDocket::set_returndate($row['returndate']);
           BookingDocket::set_journeytype($row['journeytype']);
           BookingDocket::set_passtel($row['passengertel']);
           BookingDocket::set_returndate($row['returndate']); */

            $booking_ref = BookingDocket::get_bookref();


           return json_encode(array('bookref' => $booking_ref,
                                    )
                             );

        $stmt->closeCursor();

    }

    catch (PDOException $pe) {
        die("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString);
    }

    $dbh = null;

?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title><? echo Company::get_name(); ?> :: Online Booking - Powered by</title>

</head>

<body>

<div id="cp-bookings-dialog">
  <div class="cp-tiles-wrapper-dlg">
    <div class="cp-booking-info left">
    </div>
  </div>
</div>

</body>
</html>

基本上应该发生的是,预订参考 (brData) 应该被传递给 booking-dialog.php 并在查询中使用,以通过传递的参考数据从预订数据库中选择所有预订。

我目前遇到的问题是,我在预订案卷上获得的价值是“未定义的”。从服务器发回的标头有问题,还是 JSON 对象的结构有问题?

如果有人能帮我解决这个问题,我将不胜感激,我花了很长时间试图让它发挥作用,而且看起来很简单。

4

3 回答 3

5

bookings-dialog.php不应该显示 HTML 元素,它应该返回JSON编码字符串。

添加您的结果和另一个错误,然后您可以处理来自 jQuery 的错误

例如:

<?php
    require_once('deployment.php');
    require_once('bootstrp/all.inc.php');;
    require_once('models/sql.php');
    require_once('models/bookingdocket.php');

    $pdo = new SQL();
    $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

    try {
        $rowdata = $_POST['rowdata'];
        $query = ("SELECT * FROM tblbookings WHERE bookref = :bookref");
        $stmt = $dbh->prepare($query);
        $stmt->execute(array(':bookref' => $_POST['rowdata']));
        $row = $stmt->fetch(PDO::FETCH_BOTH);

        BookingDocket::set_bookref($row['bookref']);

        echo json_encode( array('bookref' => $row['bookref'], 'date' => $row['bookingdate'], 'error' => 'no') );
        $stmt->closeCursor();
    }
    catch (PDOException $pe) {
        die(json_encode(array("error" => "Error: " .$pe->getMessage(). " Query: ".$stmt->queryString)));
    }

    $dbh = null;

?>

也在您的getGridRow检查错误字段中:

success: function(data){
    if(typeof console != "undefined"){ console.log(data); } // this will prompt data in console
    if(data.error == 'no'){
        alert("IT WORKED!");
        $('.cp-booking-info').empty();
        $('#cp-bookings-dialog').append('<p class="pno-margin">Booking Date: '+data.date+'</p>');
        $("#cp-bookings-dialog").dialog({
            show: { effect: 'drop', direction: "up" },
            hide: 'slide',
            height: 625,
            width: 733,
            title: 'Booking Reference: - '+ data.bookref
        });
    } else {
        alert(data.error);
    }
}
于 2012-08-24T09:17:21.077 回答
2

我认为唯一要改变的是更换:

title: 'Booking Reference: - '+ brData

经过 :

title: 'Booking Reference: - '+ data.bookref

brData查看undefined成功回调

于 2012-09-03T18:11:15.297 回答
1

如果您要求一个 JSON 字符串,php 应该返回那个确切的类型。Javascript 正在等待application/json内容类型。

所以在php中你需要添加一些头代码:

require_once('deployment.php');
require_once('bootstrp/all.inc.php');
require_once('models/sql.php');
require_once('models/bookingdocket.php');

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

$pdo = new SQL();
$dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

try 
{
       $rowdata = $_POST['rowdata'];
       $query = ("SELECT * FROM tblbookings WHERE bookref = '$rowdata'");

       $stmt = $dbh->prepare($query);
       $stmt->execute();

       $row = $stmt->fetch(PDO::FETCH_BOTH);

      /* BookingDocket::set_id($row['id']); */
       BookingDocket::set_bookref($row['bookref']);
      /* BookingDocket::set_bookdate($row['bookingdate']);
       BookingDocket::set_returndate($row['returndate']);
       BookingDocket::set_journeytype($row['journeytype']);
       BookingDocket::set_passtel($row['passengertel']);
       BookingDocket::set_returndate($row['returndate']); */

       $booking_ref = BookingDocket::get_bookref();

       echo json_encode(array('bookref' => $booking_ref));

       $stmt->closeCursor();

}
catch (PDOException $pe)
{
    echo json_encode(array("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString));
}
finally
{
    $dbh = null;
}
于 2012-09-03T20:51:35.827 回答