Jump to content


Photo

Using AJAX with jQuery to update a PHP variable


  • Please log in to reply
1 reply to this topic

#1 Molson31

Molson31

    Newbie

  • New Members
  • Pip
  • 6 posts

Posted 19 November 2013 - 01:53 PM

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.



#2 SocialCloud

SocialCloud

    Advanced Member

  • Members
  • PipPipPip
  • 619 posts

Posted 21 November 2013 - 09:35 AM

So you want to set a variable in PHP from JQuery? As far as I know, there is no way to do it once the page is finished loading. Is there a reason why you can't just use the variable from the js?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com