Jump to content

Entering PHP from inside of JavaScript.


Recommended Posts

My form is processed via action.php:

<?phprequire_once 'login.php';$db_server = mysql_connect($db_hostname, $db_username, $db_password);if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());$physichal_address = $_GET["physichaladdress"];$thelat = $_GET["thelat"];$thelng = $_GET["thelng"];$link = mysql_connect('localhost', 'root', '************');if (!$link) {die('Could not connect: ' . mysql_error());}mysql_close($link);?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Free Wifi locations near you:</title><link type="text/css" href="style.css" rel="stylesheet" media="all" /><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript" src="map.js"></script></head><body><h1>Free Wifi locations near you:</h1><div id="map"></div><p>If you have any questions or comments please send them to the webmaster penguin@sepserver.net .</p></body></html>

There is a referenced JavaScript in which I attempt to access PHP that fails:

(function() {//function//window.onloadwindow.onload = function() {// Creating a reference to the mapDivvar mapDiv = document.getElementById('map');// Creating a latLng for the center of the mapvar latlng = new google.maps.LatLng(37.09, -95.71);// Creating an object literal containing the properties// we want to pass to the mapvar options = {center: latlng,zoom: 4,mapTypeId: google.maps.MapTypeId.ROADMAP};//window.onload// Creating the mapvar map = new google.maps.Map(mapDiv, options);google.maps.event.addListener(map, 'bounds_changed', function() {window.bounds = map.getBounds();<?php echo "alert(\"Hello World!\");" ?>});}  })();//function

Does anyone know why my call to PHP is breaking my JavaScript? I have done this before many times but I don't see why this is failing on my IIS7 + PHP configuration. "info.php" outputs just fine! Thank you so much!

Edited by sepoto
Link to post
Share on other sites

I tried this:

window.myvar = "<?= $physichal_address ?>";

in my next javascript alert(window.myvar); I get <?= $physichal_address ?> ??????? I tried removing the quotes but that just breaks the script. Is this to do with security?

Edited by sepoto
Link to post
Share on other sites

PHP might not have short tags enabled. First try <?php echo $physichal_address; ?>. If that doesn't work then it means that your page is not executing the PHP. Remember that PHP and Javascript are not connected. PHP runs first and prints out data that Javascript can use later.

Link to post
Share on other sites

If you're pointing to an external Javascript file that you need to execute PHP in, it needs to be a .php file. You can use PHP to send a content type header for text/javascript to make sure the browser still sees it as a Javascript file.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...