Solved

php code executing on page load within  javascipt function

Posted on 2009-04-06
13
724 Views
Last Modified: 2013-12-13
Hi, I am new to php. Comingling the two types of code has become a challange.
I am trying to include some php code inside a javascript function. The php code executes when the browser loads the .php page even though it is inside the javascript function. How would I make the code execute only when the javascrpit function is called?

I tried putting the php code in a php function and that stopped the code from loading, but when I put the php code  <?php echo getll();?> in the javascript function where it was needed it still executes the code on the page load.

It seems any reference to php in the javascript function executes on page load.
Thanks.
Bob


<script language="JavaScript">

function getlatnlon(){

getlnl = GEvent.addListener(map,"click", function(overlay, latlng) {

  if (latlng) {

    latitude = parseFloat(latlng.y);

     longitude = parseFloat(latlng.x);

     latquery = (latitude  - 0.001) +"..."+(latitude  + 0.001);

     lonquery = (longitude - 0.001) +"..."+(longitude  + 0.001);
 

     <?php
 

	       $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++;

	 }
 

 ?>
 

	taxid = <php? echo $taxid ?>;

	lat = <php? echo $lat ?>;

	lon = <php? echo $lon ?>;

                      owner = <php? echo $owner ?>;
 
 

    var myHtml = "The Lat/Lon value is: " + lat + " , " + lon + " - " + taxid + " " + owner;

    map.openInfoWindow(latlng, myHtml);

    GEvent.removeListener(getlnl);
 

  }

});
 

}
 

</script>

Open in new window

0
Comment
Question by:ALASKASTREETMASTER
13 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 300 total points
ID: 24077707
It executes server side, before the page is sent to the browser. The browser can not execute php code, only javascript.
0
 

Author Comment

by:ALASKASTREETMASTER
ID: 24078073
Ok. Back to square 1. I guess the way to go about this then would be to submit the data to the php page and have it sent back to a form.  Will a php page process the request and send it back to the calling form or does it need to come back in a new page.
Thanks.
0
 
LVL 28

Assisted Solution

by:lesouef
lesouef earned 100 total points
ID: 24078342
if you know the ajax method, put the php code in an external file, and call it with a XMLHttpRequest() from within you JS function.
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 300 total points
ID: 24078625
>> Will a php page process the request and send it back to the calling form or does it need to come back in a new page.

You decide. It can go to a new page, or it can reload the page with the calling form. After the processing, send a http location header, like this:

header('Location: page.php?a=1&b=2&c='.urlencode($c_parameter));
exit;

Use urlencode() for parameters that might contain illegal characters, anything not digits, letters or a string of digits and letters. Some special characeters are allowed too, but very many are not. It is safe to use urlencode() even if the string does not contain any special characters.

If you want to use ajax, I suggest you use a specialized library, like xajax:

http://xajaxproject.org/

Ajax allows comunication between javascript and php "in the background", without reloading the page.
0
 

Author Comment

by:ALASKASTREETMASTER
ID: 24079055
I am wanting to call for the data while a user is viewing a google map. Click on the map and have the data popup in the google marker all with just one click.  Wouldn't reloading the page restart the google map page as if it was just loaded and they would loose where they were or would it reload with the new data and stay where it was?  I need it to work with the least amount of screen movement on the users end. Like click on the map to call the php page, have it process the data request and send it back to a  field in the form or into a variable without reloading the page. The original script could then get the data from a hidden field or variable and populate the popup.   Im fishing here.  Every time I go in a direction (hrs and hrs of time of course) I end up starting over because one part fails to negotiate with another.  A good solid acheivable method would be great and I would spend the time learning to get there.  Based on using the google api (javascript),  filemaker server 9 with xml or php as the data source and wanting to achieve the results as mentioned above what path would you suggest I take to get there.


Thanks
0
 
LVL 10

Assisted Solution

by:webwyzsystems
webwyzsystems earned 100 total points
ID: 24079298
Have you tried using an <IFRAME> in the existing page? It might be a bit of a chore, but at least you could leave up your map, process your requests in their very own little window on the page...as long as you name your IFRAME, you can use javascript in the parent window to get values.

This code here:
<?php ...
....
               $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++;
       }
 
 ?>

Would need to be changed to write out some javascript variables like:
                                          $row = 0;
                                          echo "<SCRIPT>";
               foreach ($records as $record) {
                    $taxid = $record->getField('TAXID_11');
                                               echo "taxID[$row]= "\"" . $taxid . "\"" ;";
                    $lat = $record->getField('LATNUM');
                                                echo "lat[$row]="\"" . $lat . "\"";";
                    $lon =  $record->getField('LONNUM');
                                                echo "lon[$row]="\"" . $lon . "\"";";
                    $owner =  $record->getField('FIRST_NAME');
                                                echo "owner[$row]="\"" . $owner . "\"";";
                    $owner2 = $record->getField('LAST_NAME');
                                                echo "owner2[$row]="\"" . $owner2 . "\"";";
                        $row++;
                                                }
echo "</SCRIPT>";

(note the little chaos there with making sure the quotes get into the script output in the echo statements)
So after it processes, the <IFRAME> window might contain something like this:

<SCRIPT>
taxID[0]="23K349";
lat[0]="somelat";
lon[0]="somelon";
owner[0]="someOwner";
owner2[0]="someOwner2";
taxID[1]="etc";
...
</SCRIPT>

Then you can just use naming conventions to get access to these variables, and use javascript how you want.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:ALASKASTREETMASTER
ID: 24081989
Thanks.  I tried putting the google map in an iframe and it locked up quite a bit when i did searches. I did not get as far as sending the code to the other iframe.  Could not figure out yet how to make only one  iframe.  I like the direction. If i could keep it from locking up I could put the return data in a frame rather than and/or in the popup. I need to do some research on the google map in an i frame to see why it is locking up and also how to keep the google map in the regular page and creating only 1 iframe for the data. more research....

The xajax would seem to be the way to go. I read through  an xajax tutorial and it seems way too easy. I am going to give it a try also. Am I understanding it that all you do is load the Ajax api, register a php function with it then call the php function with xajax in the javascript function?

Thanks again, I will resolve the question shorthly.
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 300 total points
ID: 24082075
>> Am I understanding it that all you do is load the Ajax api, register a php function with it then call the php function with
>> xajax in the javascript function?

Yes, it is very easy to use.
0
 

Author Comment

by:ALASKASTREETMASTER
ID: 24082287
I have been all over the ajax website and do not see instructions for installing the ajax zip files.
no installer no instructions on where to put them.  any ideas.
0
 

Author Closing Comment

by:ALASKASTREETMASTER
ID: 31567034
thank you all for your input I know this question leads to many more.  I will  bring up a new question on my new issues.
Bob
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24082677
Just install it in a folder below your root folder or project folder. I called the folder "xajax" and put it on site root.
0
 

Author Comment

by:ALASKASTREETMASTER
ID: 24082813
will it load automatically?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24082903
No. You must include a php file, and possibly configure the paths.

http://xajaxproject.org/en/docs-tutorials/learn-xajax-in-10-minutes/

Between step 2 and step 3:

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

In this example ../xajax/ is the path from the project folder to where I installed Xajax.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Having just upgraded from Filemaker 11 to Filemaker 12 over the weekend, we thought we would add some tips for others making the same move.  In general, our installation went without incident. Please note that this is not a replacement for Chapter 5…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

758 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

18 Experts available now in Live!

Get 1:1 Help Now