Solved

php xajax and javascript variable exchange

Posted on 2009-04-07
8
1,399 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now