Solved

php xajax and javascript variable exchange

Posted on 2009-04-07
8
1,500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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