传递Jquery变量以在php脚本中使用

时间:2012-07-10 08:45:38

标签: php jquery

我有一个预订网格,如下所示:

enter image description here

在这个预订网格上,我有一个dblClick事件,它打开了一个Jquery对话框:

<div id="cp-bookings-dialog">

    <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">Return Date: &nbsp;<strong><? echo BookingDocket::get_bookdate(); ?></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>

 </div>

Jquery代码:

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

        getGridRow(brData);

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

其中brData是我想在PHP脚本中使用的“预订参考”值。目前,此dblClick事件正在发送到以下Ajax请求:

function getGridRow(brData) {

   $.ajax({

    url: 'scripts/php/bootstrp/all.request.php',
    type: 'POST',

    data: {

        rowdata: brData,

        id: null,
        condition: null
    },
    dataType: 'text/xml',
    timeout: 20000,
    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(response){

        // Refresh page

        //response = brData;
        //alert(response);  <--- This alerts the correct Booking Reference

    }
});

将其发送至all.request.php

// Switch to determine method to call
switch ($_REQUEST['fnme']) {

case 'getDGRow':
header('Content-type: text/xml');
GetBookings::getGridRow($_REQUEST['rowdata']);
break;

最后到我想要使用这个Jquery值的PHP脚本:

class GetBookings {

public static function getGridRow($rowdata) {

    $rowdata = $_REQUEST['rowdata'];

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

    try {

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

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

            $stmt->execute();

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

            {variables are all here}

        $stmt->closeCursor();

    }

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

    $dbh = null;

    }


    }

我不确定为什么,但这似乎不起作用。另外,请注意,当您点击预订时,这将打开一个jQuery对话框,其中显示有关预订的所有详细信息,并且应该返回$query = "SELECT * FROM tblbookings WHERE bookref = '$rowdata'";

的所有结果

我需要做的就是正确传递这些数据,这就好像我用预订参考值替换$rowdata一样,这样可以正常工作。

如果有人可以帮助我,我将非常感激,因为我一直试图让这个工作相当长一段时间:(

2 个答案:

答案 0 :(得分:1)

为什么要发送

 data: {

    rowdata: 'fnme=getDGRow&row_data='+brData,

    id: null,
    condition: null
},

而不喜欢

 data: {

    fname: 'getDGRow',
    rowdata: brData,

    id: null,
    condition: null
},

在php文件中,您可以使用$_POST['fname']$_POST['rowdata']来获取单个值。

目前,$_REQUEST['rowdata']是您的整个字符串'fnme=getDGRow&row_data='+brData

如果要在mysql查询中使用brData,请尝试使用$_REQUEST['row_data']或使用上面的示例将变量$_REQUEST['rowdata']设置为正确。


您应该使用的整个代码

function getGridRow(brData) {

   $.ajax({

    url: 'scripts/php/bootstrp/all.request.php',
    type: 'POST',

    data: {

        fnme: 'getDGRow',
        rowdata: brData,

        id: null,
        condition: null
    },
    dataType: 'text/xml',
    timeout: 20000,
    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(response){

        // Refresh page

        //response = brData;
        alert(response); // <--- This alerts the correct Booking Reference

    }
});

-

// Switch to determine method to call
switch ($_REQUEST['fnme']) {

case 'getDGRow':
header('Content-type: text/xml');
GetBookings::getGridRow($_REQUEST['rowdata']);
break;

-

class GetBookings {

public function getGridRow($rowdata) {

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

    try {

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

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

            $stmt->execute();

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

            {variables are all here}

        $stmt->closeCursor();

    }

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

    $dbh = null;

    }


    }

答案 1 :(得分:0)

查看您的代码:

var rowData = new Array();
    rowData = $("#bookings").getRowData(rowid);
    var brData = rowData['bookref'];

第二行覆盖了第一行,因此您可以删除rowData = new Array()

现在,您确定$_REQUEST['fnme']存在吗?您的switch语句没有任何默认值,看起来永远不会执行getGridRow()

另外,$rowdata = $_REQUEST['rowdata']没有意义,你可以删除它。

我希望它可以帮到你。