Jump to content

Using AJAX with jQuery to update a PHP variable


Molson31

Recommended Posts

Hello. I am having a disaster of a time trying to solve this. I am trying to update a PHP variable by using AJAX within a jQuery function. It's been two days and I am having no luck. I am also using jQgrid with is a jQuery API. Here is my situation:

 

jQgrid is a jQuery API that looks like an excel sheet table (rows and columns, etc). I have a button that creates a PDF based on the row chosen. The rowID is determined based on a jQuery/jQgrid function from the API. I want to get that result out of the jQuery function and store it within the original page in a variable called $resultId. 

 

Main PHP page

//------Custom PDF button------------//
$getUnitID = <<<getUnitID //The jQuery function is created like this so that it can be called from another function in the API as seen at the end
function(rowid, selected)
{
	var selr= null;
	if(rowid != null){ 
		selr = jQuery('#grid').jqGrid('getGridParam','selrow'); //This is the API telling me what row I have selected
		$.ajax({
			type: "POST",
			url: "getId.php", //Supposedly I should be posting the result to another php page
			dataType: "json",
			data: {selr:selr},      
			success: function(data) {
			alert (selr) //This will accurately tell me what row I selected. But instead of an alert, I want it stored as a variable
			
			}
		}); 
getUnitID; //end of Jquery function
$grid->setGridEvent('onSelectRow',$getUnitID); //From the jQgrid API, this is how the jQuery function is called (using the custom OnSelectRow function)
//$resultId =0; //This is the var I want t o set

$pdfButton = array("#pager",array("caption"=>"Create PDF", "onClickButton"=>"js: function(){parent.location='/pdftkphp/example/download.php?id=' ". $resultId ."}")); //This is the button, which is just a link and concats the $resultId 

$grid->callGridMethod("#grid", "navButtonAdd", $pdfButton); //Simply how the button is added to the jqGrid
//-------------------------------------//

getId.php

<?php
if (isset($_POST["selr"]))
{
	$rId = $_POST["selr"];
	echo $rId;
}
?>

So all this does is execute the script, sends it to the other php page, and returns the result as an alert. Instead of an alert, I wish to have the result stored as a variable. Then that variable can be passed to the button link.

 

The problem, I think, is that once the PHP page is loaded, it's done, right? I cannot change $resultId.. but, there must be a way. I am thinking, does the whole entire main PHP page need to be loaded in an AJAX function? 

 

Basically, the variable I want is stuck inside that JQuery function to (getUnitId). I just want the result of the function to be passed back to the same page I am in or use some work-around to do so.

 

Thank you so much.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.