• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1550
  • Last Modified:

php xajax and javascript variable exchange

I have gotten my feet wet with xajax.  I am having a hard time understand the exchange of variables and when they are available and to what they are available.
In my function that i am regestering with xajax i need two variables from javascript that are already defined when the xajax function is called afterwards in the same script. I am callin the php/xajax function to query data from the server. In the function I am converting the javascript variables with  the following.  

$latquery =  $_GET['latquery'];
$lonquery =  $_GET['lonquery'];

Is the syntax correct for getting the already defined javascript variables?  Does the xajax function convert the variables before sending the php function to the server, thus having the values it needs when performing the rquest.

I think I have all the parts in place for the script to work, at least so far the page loads without an error except when I add step 5 and 6 from the xajax turorial.

//////Does step 5 go in the php function?/////
5. Before your script sends any output, have xajax handle any requests:
Code: php
 $xajax->processRequest();
 
/// I put step 6 in the header, but it errors. Where does it go?//////
6. Between your tags, tell xajax to generate the necessary JavaScript:
Code: php
 <?php $xajax->printJavascript(); ?>

I have not figured out how to get the returned data back into a javascript variable, that will be next. Need to get the query working first.
Thanks.
<script language="JavaScript">
 <?php
    function getll($arg){
 
	       $latquery =  $_GET['latquery'];
	       $lonquery =  $_GET['lonquery'];
	   	 require_once("FileMaker.php");
	   	 $fm = new FileMaker('Cama');
	   	 $findCommand =& $fm->newFindCommand('ASMLATLON');
	      $findCommand->addFindCriterion('LONNUM',$lonquery);
	      $findCommand->addFindCriterion('LATNUM',$latquery);
	   	 $result = $findCommand->execute();
	   	 $records = $result->getRecords();
 
	   	$row = 0;
 
	   	foreach ($records as $record) {
 
	   	     $taxid = $record->getField('TAXID_11');
	   	     $lat = $record->getField('LATNUM');
	   	     $lon =  $record->getField('LONNUM');
	   	     $owner =  $record->getField('FIRST_NAME');
	   	     $owner2 = $record->getField('LAST_NAME');
	  	    	$row++;
	 }
 
$newContent = "Value of $arg: ".$arg;
$objResponse = new xajaxResponse();
$objResponse->assign("SomeElementId","innerHTML", $newContent);
return $objResponse;
$xajax->processRequest();
 
 
 
}
 ?>
 
<?php
require_once("xajax/xajax_core/xajaxAIO.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/xajax_js/xajax_core.js');
$xajax->registerFunction("getll");
?>
</script>  
//where do these two steps go?//
5. Before your script sends any output, have xajax handle any requests:
Code: php
 $xajax->processRequest();
 6. Between your tags, tell xajax to generate the necessary JavaScript:
Code: php
 <?php $xajax->printJavascript(); ?>

Open in new window

0
ALASKASTREETMASTER
Asked:
ALASKASTREETMASTER
  • 4
  • 4
1 Solution
 
Roger BaklundCommented:
Line 41:

$xajax->configure('javascript URI','xajax/xajax_js/xajax_core.js');

It should be just:

$xajax->configure('javascript URI','xajax/');

$xajax->processRequest(); should be executed early in your script, before any output. Not within the handler function, like you have it now.

<?php $xajax->printJavascript(); ?>

This outputs the javascript, it should be placed in html head.

You can set variables with the script method:

  $objResponse = new xajaxResponse();
  $objResponse->assign("SomeElementId","innerHTML", $newContent);
  $objResponse->script("x=123;y=321;");
0
 
ALASKASTREETMASTERAuthor Commented:
when I put <?php $xajax->printJavascript(); ?> in the page header, the page won't load and get the error 'undefined variable xajax' and call to member function printJavascript() on non object.
0
 
Roger BaklundCommented:
You must create the $xajax object before it is used. This works for me:
<?php
 
require_once("xajax/xajax_core/xajax.inc.php");
 
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
 
$xajax->registerFunction("myFunction");
 
$xajax->processRequest();
 
function myFunction($arg) {
  $newContent = 'Value of $arg: '.$arg;
  $objResponse = new xajaxResponse();
  $objResponse->assign("SomeElementId","innerHTML", $newContent);
  return $objResponse;
}
 
 
?><html><head><title>Xajax test</title>
<?php $xajax->printJavascript(); ?>
<script type="text/javascript">x=0;</script>
</head><body>
<div id="SomeElementId"></div>
<button onclick="xajax_myFunction('It worked!');">test</button>
</body>
</html>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ALASKASTREETMASTERAuthor Commented:
Thanks, correct orderworks...
I did a test with just a php page and was able to echo the values from the database. When I put the code in my map page. the script works (no errors anyway) but when I call for the taxid variable as converted below in the google marker popup it comes up 'undefined'. It must not be making it back. Is the syntax correct on the objresponse below? I created a link to just perform xajax_getll() by itselt and it opens up in another page and says 'false' . I do not get that or any other error when calling xajax_getll(); from my javascript. Stumped.

$newContent = "Value of $arg: ".$arg;
$objResponse = new xajaxResponse();
$objResponse->script("taxid = $taxid");
return $objResponse;


Thanks

  <?php
    function getll($arg){
 
	 $lonquery =  '-149.8375..-149.8373';
	  $latquery =  	'61.12851..61.12871';
	 		  	   	 require_once("FileMaker.php");
	 		  	   	 $fm = new FileMaker('Cama');
	 		  	   	 $findCommand =& $fm->newFindCommand('ASMLATLON');
	 		  	      $findCommand->addFindCriterion('LONNUM',$lonquery);
	 		  	      $findCommand->addFindCriterion('LATNUM',$latquery);
	 		  	   	 $result = $findCommand->execute();
	 		  	   	 $records = $result->getRecords();
 
	 		  	   	$row = 0;
 
	 		  	   	foreach ($records as $record) {
 
	 		  	   	     $taxid = $record->getField('TAXID_11');
	 		  	   	     $lat = $record->getField('LATNUM');
	 		  	   	     $lon =  $record->getField('LONNUM');
	 		  	  	    	$row++;
	 		}
 
 
$newContent = $arg;
$objResponse = new xajaxResponse();
$objResponse->script("taxid = $taxid");
return $objResponse;
 
 
 
 
}
 ?>
 
<?php
require_once("xajax/xajax_core/xajaxAIO.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
$xajax->registerFunction("getll");
$xajax->processRequest();
$xajax->bDebug = true;
 
?>

Open in new window

0
 
Roger BaklundCommented:
Are you sure the query finds any records? Transfer some other variable, for instance $row, and use alert() to see the transfered value:

$objResponse = new xajaxResponse();
$objResponse->script("row=$row;taxid = $taxid;alert('row=$row;taxid = $taxid');");
return $objResponse;
0
 
ALASKASTREETMASTERAuthor Commented:
when i run the query in a php page and use echo $taxid instead of the objrequest lines it prints the taxid from the database, so the query does work.

I entered the  line:
$objResponse->script("row=$row;taxid = $taxid;alert('row=$row;taxid = $taxid');");

and it made no difference.  Both row and taxid are undefined.

Its af if putting the
xajax_getll()

has no affect, like its not being called.

what is the correct way to register and call the xajax function.
$xajax->registerFunction("getll");
or
$xajax->registerFunction("getll()");

also callin the the javascript.
xajax_getll();
or
xajax_getll;


  <?php
    function getll($arg){
 
	 $lonquery =  '-149.8375..-149.8373';
	  $latquery =  	'61.12851..61.12871';
	 		  	   	 require_once("FileMaker.php");
	 		  	   	 $fm = new FileMaker('Cama');
	 		  	   	 $findCommand =& $fm->newFindCommand('ASMLATLON');
	 		  	      $findCommand->addFindCriterion('LONNUM',$lonquery);
	 		  	      $findCommand->addFindCriterion('LATNUM',$latquery);
	 		  	   	 $result = $findCommand->execute();
	 		  	   	 $records = $result->getRecords();
 
	 		  	   	$row = 0;
 
	 		  	   	foreach ($records as $record) {
 
	 		  	   	     $taxid = $record->getField('TAXID_11');
	 		  	   	     $lat = $record->getField('LATNUM');
	 		  	   	     $lon =  $record->getField('LONNUM');
	 		  	  	    	$row++;
	 		}
 
 
$newContent = $arg;
$objResponse = new xajaxResponse();
$objResponse->script("row=$row;taxid = $taxid;alert('row=$row;taxid = $taxid');");
return $objResponse;
 
 
 
 
}
 ?>
 
<?php
require_once("xajax/xajax_core/xajaxAIO.inc.php");
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
$xajax->registerFunction("getll");
$xajax->processRequest();
$xajax->bDebug = true;
 
?>

Open in new window

0
 
ALASKASTREETMASTERAuthor Commented:
I will follow up in a new question.  thanks
0
 
Roger BaklundCommented:
This one is correct:

$xajax->registerFunction("getll");

>> and it made no difference.  Both row and taxid are undefined.

Do you get the javascript alert? What does it say? 'row=undefined;taxid = undefined' or 'row=;taxid =' or something else?

Lines 34-36 in the snippet... is it like that in your script? This sends a linefeed to the browser, and should not be there. The script should start with the lines 37-42.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now