Output PHP with JavaScript

Hi everyone, I hope you can help.
I am trying to use Google maps with information windows. I would like to add some php data into the info window. Can this be done? I am adding html into the info window successfully, but I try and do <?= $somePHPvar; ?> within the html, and this is what is not working.
My Javascript is in a separate .js file, and my php is above the html in the index.php file.

Thank you.

You’ve probably thought of this already, but do you have the short tags <? ... ?> enabled for your version of PHP?

Hi. Yes I do, it’s a local Vagrant LAMP stack using PHP 5.5. I have also tried <?php ?>

I am by no means an expert, but PHP is surely a server-side language, so by the time your page is with the browser for display, the PHP side has all been done. So I don’t think you’ll be able to call PHP from Javascript, unless you employ Ajax calls to execute the PHP when the JS needs to.

I am hoping I can save retrieved data in a php variable and then pass it to the content part of the Google maps information window, but the maps is in javascript.

Is there a way you could assign the value that you need to a non-displayed part of the page, then use your javascript to retrieve the value from there for use with Google Maps?

Or, show us some code.

Either that or if you don’t know the value until after your visitor starts interacting with the page then use an ajax call to retrieve it from the server.

So lets draw together droop and fel’s posts:

Execution Times:
PHP executes on the server. It parses the PHP file, and exports pure HTML to the client. This ends PHP’s execution cycle.
Javascript executes on the client. It interacts with the HTML that the client has received. It cannot() interact with PHP.
(
: It can interact with the HTML output of AJAX requests to PHP pages.)

#If the data is known at page-load time
Store the data in a javascript variable inside your script:

var storeddata = “<?php echo $data; ?>”

When the client receives the HTML (IE: If you View Source), it will see:

var storeddata = “This is a string with some stuff in it”

And from there, you can use it like you do any other javascript variable.

#If the data is reliant upon client interaction
You’ll need to AJAX call from javascript, calling your PHP and passing whatever relevant variables need to be passed.
Use javascript to parse the result of the AJAX call.

So, the first step would be to just see if the data you need is in fact echoed in the page. I would try something like:

index.php

<?php 
... some php code here...
?>

My variable: <?= $yourVariable ?>

... some other HTML / JS code here

If this doesn’t work, then maybe you have an error in your logic assigning a value to ‘$yourVariable’.

If this does work, then as @StarLion mentioned, you can use it normally in your index.php file like this:

<?php 
... some php code here...
?>
    
... some other HTML / JS code here

<script>
var myVar = "<?= $myVariable ?>";
alert(myVar);
// Other JS code to add the text to Google Ma
</script>

Thanks for the replies, I’ll have another look and let you know.

Yes it can be done.
http://mx2.wifigator.com/open-mesh/

Has the v3 infowindows with dynamic content from the database.
There is a tiny link at the top to Click here to view as administrator.
You will have to view source to see what is going on
Notice how the image in the info window changes once you log in.

Zoom in and you will see where the polylines come in.

As admin you can also drag the nodes around and they will stay put.

Come to think about it but I think I am using the infoBubble instead of the info window but the idea is the same.