Solved

php xajax and javascript variable exchange

Posted on 2009-04-07
8
1,458 Views
Last Modified: 2013-12-13
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
Comment
Question by:ALASKASTREETMASTER
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24088132
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
 

Author Comment

by:ALASKASTREETMASTER
ID: 24088788
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24088875
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:ALASKASTREETMASTER
ID: 24089849
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
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24090269
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
 

Author Comment

by:ALASKASTREETMASTER
ID: 24090796
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
 

Author Closing Comment

by:ALASKASTREETMASTER
ID: 31567538
I will follow up in a new question.  thanks
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24091237
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question