将Jquery值传递给PHP Query

时间:2012-08-17 14:07:34

标签: php jquery ajax

我有一个带有OndblClickRow函数的JQGrid,如下所示:

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



        getGridRow(brData);

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

这将传递给以下函数:

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

加载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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<?php

set_include_path($_SERVER['DOCUMENT_ROOT'] . "/onlinebookingv4/scripts/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']);



        $stmt->closeCursor();

    }

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

    $dbh = null;





?>
</head>

<body>
<div class="cp-tiles-wrapper-dlg">

<div class="cp-booking-info left">

    <p class="pno-margin">Booking Date: &nbsp;<strong>Booking Reference is = <? echo BookingDocket::get_bookref(); ?></strong></p>
    <p class="pno-margin">Query =  &nbsp;<strong><? echo $query; ?></strong></p>
    <p class="pno-margin">Journey: &nbsp;<strong></strong></p>
    <p class="pno-margin">Passenger Tel: &nbsp;<strong></strong></p>
    <p class="pno-margin">E-mail: &nbsp;<strong></strong></p>

</div>

</div>
</body>
</html>

我要做的是传递'brData',它是bookings-dialog.php的预订参考,以便我可以通过预订参考从数据库中选择所有预订。

目前,当我双击一行时,在firebug控制台中我得到以下内容:

POST bookings-dialog.php,显示正确的SELECT * FROM tblbookings WHERE bookref = 'BR1278'

但它也显示出来:

获取显示SELECT * FROM tblbookings WHERE bookref = ''

的bookings-dialog.php

对我来说,这似乎被某种方式覆盖了,我可能错了。

任何人都知道如何才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

我们不想发送回HTML,我们想要发送回JSON等内容。我们的#cp-bookings-dialog元素应该具有类似于 - &gt;

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

然后我们将数据发送到服务器。你现有的代码实际上很好,我的评论假设采取了不同类型的行动。

$.post("bookings-dialog.php", { 'rowdata': brData } );

这是我必须做出假设的地方。在set_var($_POST['var'])类上执行BookingDocket之后,我们应该返回一个JSON编码的键:值对数组,以便在我们的浏览器中进行解析,并用于更新对话框中的信息。

//for example, only showing the last two...
BookingDocket::set_passtel($row['passengertel']);
BookingDocket::set_returndate($row['returndate']);

//create a scope of variables to prepare and return
$booking_date = BookingDocket::get_bookref();
//query is already set, omitting it.
//making assumptions about data I cannot see
$journey = BookingDocket::get_journey();
$pass_tel = BookingDocket::get_tel();
$email = BookingDocket::get_email();

//formatting here is for example and not live usage
return json_encode(array("date" => $booking_data, 
                         "query" => $query, 
                         "journey" => $journey, 
                         "pass_tel" => $pass_tel, 
                         "email" => $email
                         )
                  );

所以现在我们返回一个JSON编码数组,让我们回到$ .post方法,看看我们现在能做些什么。

//I prefer to write things out with $.ajax
$.ajax({
  url: 'booking-dialog.php',
  data: {'rowdata' : brData },
  dataType: 'json', //this is what we expect our returned data as
  success: function(data){
    //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-booking-info').append('<p class="pno-margin">Booking Date: '+data.date+'</p>');
    //the query
    $('.cp-booking-info').append('<p class="pno-margin">Query: '+data.query+'</p>');
    //the journey
    $('.cp-booking-info').append('<p class="pno-margin">Journey: '+data.journey+'</p>');
    //thepassenger tel
    $('.cp-booking-info').append('<p class="pno-margin">Passenger Tel: '+data.pass_tel+'</p>');
    //the email
    $('.cp-booking-info').append('<p class="pno-margin">Email: '+data.email+'</p>');

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

});

这是我可以想象你的系统如何工作的最快的模型。