Yes, there are lots of topics about this, but no one really posts the full code so I do not understand what to do. I would appreciate help. I am using some custom API (jqgrid) and will explain what some things do.
This is how it goes:
First there is a PHP function
$getUnitID = <<<getUnitID //This is how you start a javascript function in this API
function(rowid, selected)
{
var selr= null;
if(rowid != null){
selr = jQuery('#grid').jqGrid('getGridParam','selrow');
return selr;
} //All this does is give me a value based on a row I click. It works great.
}
getUnitID; // End of JS function
Now I know I need to execute some AJAX. But I don't know when or where or how. I know about reading the manual but that doesn't always help. I am still lost (it's been 3 hours). Here is what I gather should be the AJAX, but where does it go?
$.ajax({
type: "POST",
url: "webpage.php",
dataType: "json",
data: selr,
success: function(data) {
alert (data);
}
});
I don't know how to implement that AJAX call.
The final goal is to place the data from var "selr" into a simple php function such as $myVariable.
This is how I've tried to combine
$getUnitID = <<<getUnitID
function(rowid, selected)
{
var selr= null;
if(rowid != null){
selr = jQuery('#grid').jqGrid('getGridParam','selrow');
//alert (selr);
return selr;
}
$.ajax({
type: "POST",
url: "getId.php",
dataType: "json",
data: {selr:selr},
success: function(data) {
alert (data);
}
});
}
getUnitID;
$grid->setGridEvent('onSelectRow',$getUnitID);
$pdfButton = array("#pager",array("caption"=>"Create PDF", "onClickButton"=>"js: function(){parent.location='/pdftkphp/example/download.php?id= ". 6 ." '}"));
And the seperate PHP page is just
<?php
$rId = $_POST["selr"];
echo $rId + "some ajax stuff";
?>
So, hopefully this helps. In your AJAX call, your url
is calling webpage.php - so presumably that page should be waiting for an incoming variable. Something like
$myVariable = $_POST["selr"];
So, that above statement is checking the POST
for a variable called selr
, well, we have to tell the AJAX call to send your variable as selr
, so change your data
in the AJAX call to be a key/val pair:
data: {selr:selr},
Now, that success
function with the parameter data
is the data you echo
back from your PHP side. So say you have:
$myVariable = $_POST["selr"];
echo $myVariable + "some ajax stuff";
Your data
will now contain the output of that echo
. Hope this was helpful.