PHP/Ajax/jQuery——将jQuery值传递给PHP脚本

时间:2021-01-08 17:03:47

I have a bookings.php page which has a jqgrid that displays all the bookings that have been made online. When you double click a row, this opens a jq dialog that displays all the details about there booking. Also, when you double click, I have a variable defined which is the booking reference which I want to pass to a php script:

我有一个预订。php页面,它有一个jqgrid,显示所有在线预订。当您双击一行时,这将打开一个jq对话框,显示所有关于预订的详细信息。另外,当你双击时,我定义了一个变量,它是我想传递给php脚本的预订引用:

var brData = rowData['bookref'];

I am sending this variable via ajax:

我通过ajax发送这个变量:

function getGridRow(brData) {

   $.ajax({

    // Request sent from control panel, so send to cp.request.php (which is the handler)
    url: 'scripts/php/bootstrp/all.request.php',
    type: 'GET',

    // Build data array - look at the '$_REQUEST' parameters in the 'insert' function
    data: {


        //ft: "getDGRow",
        rowdata: 'fnme=getDGRow&row_data='+brData,
        data: brData,

        // Either pass a row id as the 'id' OR a where clause as the 'condition' never both
        id: null,
        condition: null
    },
    dataType: 'text',
    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);

    }
});


}

Here is the switch case for all.inc.php:

以下是all.inc.php的切换情况:

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

This is the PHP function that I am sending the jquery variable to, to use within my PHP code:

这是我要发送jquery变量到的PHP函数,在我的PHP代码中使用:

public static function getGridRow($rowdata) {

    $rowdata = $_GET['data'];
    echo $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);

        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;

}


}

I have put echo $rowdata; in the PHP function to see if the variable is being passed which it is as I can see 'BR12345' in the firebug console. The problem is that this query:

我输入echo $rowdata;在PHP函数中,查看是否正在传递变量,如我在firebug控制台中看到的“BR12345”。问题是这个查询:

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

is not fetching any results. If I was to put:

不获取任何结果。如果我说:

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

it does fetch the results that I need so I can't understand why this query isn't working when the variable brData is being passed to $rowdata

它确实获取了我需要的结果,所以当变量brData被传递给$rowdata时,我无法理解为什么这个查询不能工作

Any suggestions?

有什么建议吗?

5 个解决方案

#1


0  

Try to aislate your problem first. You say you have no problem with firebug, try to put here the console.dir() response to validate.

先把你的问题写下来。您说您对firebug没有问题,请尝试将console.dir()响应放在这里进行验证。

Mean while do the following:

同时做以下事情:

Then see yor $_REQUEST var with a print_r(). Is your variable there?. If so, do a var_dump($_REQUEST['rowdata']) and check.

然后使用print_r()查看您的$_REQUEST var。是你的变量?。如果是,则执行var_dump($_REQUEST['rowdata'])并检查。

In public static function getGridRow($rowdata) see that you overwrite $rowdata to see the echo. And finally if you have all alright by now prepare correctly your query

在公共静态函数getGridRow($rowdata)中,您将覆盖$rowdata以查看echo。最后,如果你已经准备好了,现在就准备好你的查询。

#2


2  

Wondering why you have a prepared statement in your code but not actually using it properly.

想知道为什么在代码中有一个准备好的语句,但实际上没有正确地使用它。

$stmt = $dbh->prepare("SELECT * FROM tblbookings WHERE bookref = :data");
$stmt->execute(array(
    ':date' => trim($rowdata),
));

I've added trim() to make sure there are no spaces or newlines around it that could mess things up.

我已经添加了trim(),以确保它周围没有空格或换行符会把事情弄糟。

Update

更新

It's debug time:

调试时间:

public static function getGridRow($rowdata) {

    $rowdata = $_GET['data'];
    echo $rowdata;

Add the following lines:

添加以下行:

    echo "=====DEBUG====== ";
    var_dump($rowdata); 
    echo " =====DEBUG====== ";
    exit;

This will write the value and immediately stop your script so you can inspect its value in detail.

这将写入值,并立即停止脚本,以便您可以详细检查它的值。

#3


1  

Use functions

使用函数

HtmlSpecialChar() 
Trim()

then display $rowdata variable and if string is in correct format then

然后显示$rowdata变量,如果字符串是正确的格式。

try this

试试这个

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

or

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

PHP can see variable without -> '

PHP可以看到没有-> '的变量

#4


0  

My answer is wrong, I don't delete it so no one else posts this wrong answer
Proof I am wrong: http://codepad.org/fvHM81Uh

我的答案是错误的,我没有删除它,所以没有人张贴这个错误的答案证明我是错误的:http://codepad.org/fvHM81Uh

Try

试一试

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

$query =(“从bookref = '的tblbooking中选择*”)。构成了rowdata。美元“的”);

In PHP vars in strings are handlet so:

在PHP vars中,字符串是handlet,所以:

$variable = "Hello";
echo "$variable"; //=> Hello
echo '$variable'; //=> $variable

BUT:

但是:

 echo "'$variable'"; //=> 'Hello'

#5


0  

Not really sure form the above whether $rowdata is an array, but I'm assuming it's not. In that case, have you tried:

我不确定$rowdata是否是一个数组,但我假设它不是。既然如此,你试过:

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

#1


0  

Try to aislate your problem first. You say you have no problem with firebug, try to put here the console.dir() response to validate.

先把你的问题写下来。您说您对firebug没有问题,请尝试将console.dir()响应放在这里进行验证。

Mean while do the following:

同时做以下事情:

Then see yor $_REQUEST var with a print_r(). Is your variable there?. If so, do a var_dump($_REQUEST['rowdata']) and check.

然后使用print_r()查看您的$_REQUEST var。是你的变量?。如果是,则执行var_dump($_REQUEST['rowdata'])并检查。

In public static function getGridRow($rowdata) see that you overwrite $rowdata to see the echo. And finally if you have all alright by now prepare correctly your query

在公共静态函数getGridRow($rowdata)中,您将覆盖$rowdata以查看echo。最后,如果你已经准备好了,现在就准备好你的查询。

#2


2  

Wondering why you have a prepared statement in your code but not actually using it properly.

想知道为什么在代码中有一个准备好的语句,但实际上没有正确地使用它。

$stmt = $dbh->prepare("SELECT * FROM tblbookings WHERE bookref = :data");
$stmt->execute(array(
    ':date' => trim($rowdata),
));

I've added trim() to make sure there are no spaces or newlines around it that could mess things up.

我已经添加了trim(),以确保它周围没有空格或换行符会把事情弄糟。

Update

更新

It's debug time:

调试时间:

public static function getGridRow($rowdata) {

    $rowdata = $_GET['data'];
    echo $rowdata;

Add the following lines:

添加以下行:

    echo "=====DEBUG====== ";
    var_dump($rowdata); 
    echo " =====DEBUG====== ";
    exit;

This will write the value and immediately stop your script so you can inspect its value in detail.

这将写入值,并立即停止脚本,以便您可以详细检查它的值。

#3


1  

Use functions

使用函数

HtmlSpecialChar() 
Trim()

then display $rowdata variable and if string is in correct format then

然后显示$rowdata变量,如果字符串是正确的格式。

try this

试试这个

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

or

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

PHP can see variable without -> '

PHP可以看到没有-> '的变量

#4


0  

My answer is wrong, I don't delete it so no one else posts this wrong answer
Proof I am wrong: http://codepad.org/fvHM81Uh

我的答案是错误的,我没有删除它,所以没有人张贴这个错误的答案证明我是错误的:http://codepad.org/fvHM81Uh

Try

试一试

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

$query =(“从bookref = '的tblbooking中选择*”)。构成了rowdata。美元“的”);

In PHP vars in strings are handlet so:

在PHP vars中,字符串是handlet,所以:

$variable = "Hello";
echo "$variable"; //=> Hello
echo '$variable'; //=> $variable

BUT:

但是:

 echo "'$variable'"; //=> 'Hello'

#5


0  

Not really sure form the above whether $rowdata is an array, but I'm assuming it's not. In that case, have you tried:

我不确定$rowdata是否是一个数组,但我假设它不是。既然如此,你试过:

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