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:
var brData = rowData['bookref'];
I am sending 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, to use within 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 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:
$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
Any suggestions?
ajax({ url: 'ajax. php', //This is the current doc type: "POST", data: ({name: '145'}), //variables should be pass like this success: function(data){ console. log(data); } }); $.
jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!
All you need to do to use jQuery with PHP is to include the jQuery javascript file in your HTML document in the head tag. I actually use PHP along with jQuery all the time.
Start Using AJAX Today In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.
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.
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.
Use functions
HtmlSpecialChar()
Trim()
then display $rowdata variable and if string is in correct format then
try this
$query = ("SELECT * FROM tblbookings WHERE bookref = '$rowdata'");
or
$query = ("SELECT * FROM tblbookings WHERE bookref = '".$rowdata."'");
PHP can see variable without -> '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With