PHP / Ajax / jQuery - passing a jquery value to a PHP script

I have a bookings.php page that has a jqgrid that displays all the orders that were made on the Internet. When you double-click on a line, the jq dialog opens, which displays all the booking details. In addition, when you double-click, I have a specific variable, which is a link to the reservation that I want to pass to the php script:

var brData = rowData['bookref']; 

I send this variable via 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:

 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 for use in my PHP code:

 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 put echo $ rowdata; in a PHP function to find out if a variable is being passed, which I see in BRB4345 in the firebug console. The problem is that this request:

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

does not get any results. If I were to put:

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

it retrieves the results that I need, so I can’t understand why this query does not work when the brData variable is passed to $ rowdata p>

Any suggestions?

+6
source share
5 answers

First try to crash your problem. You say that you have no problems with firebug, try putting console.dir() answer here for verification.

The average time does the following:

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

In the public static function getGridRow($rowdata) see that you overwrite $rowdata to see the echo. And finally, if everything is all right with you, now properly prepare your request

0
source

It is amazing why you have a prepared statement in your code, but in fact it is not used correctly.

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

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

Update

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 that you can examine its value in detail.

+2
source

Use functions

 HtmlSpecialChar() Trim() 

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

try it

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

or

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

PHP can see the variable without β†’ '

+1
source

My answer is incorrect, I do not delete it, so no one else sends this incorrect answer
Proof: I'm wrong: http://codepad.org/fvHM81Uh

Try

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

In PHP vars, strings have a handlet, so:

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

BUT:

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

Not quite sure what is above: $ rowdata is an array, but I guess not. In this case, you tried:

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

Source: https://habr.com/ru/post/917596/


All Articles