Solved

php xajax and javascript variable exchange

Posted on 2009-04-07
8
1,415 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why isnt it sending mail from my php but is from my server 10 35
File submission to SQL Server 1 38
Please show me easiest post form. 3 31
is this a cms? 8 34
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

912 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

22 Experts available now in Live!

Get 1:1 Help Now