Friday, 15 April 2011

Accessing $_POST variables in PHP from JavaScript -



Accessing $_POST variables in PHP from JavaScript -

i've made android app sends 2 strings of latitude , longitude values on http post, taken php code , stored in variables. variables should then, in theory, taken in javascript code. however, while know php code getting strings since can write them different html file, whenever seek alert() value of javascript variable stored string, blank alert. here php , javascript code. help appreciated!

<?php $latitude = $_post["latitude"]; $longitude = $_post["longitude"]; $datetime = date('m/d/y h:i:s a'); file_put_contents("locationdata.html", $latitude, file_append); ?> <html> <title>find location</title> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0; padding: 0 } #map-canvas { height: 100% } #map-canvas { width: 100% } </style> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=(my api key removed privacy)"> </script> <script type="text/javascript"> var latitude = "<?php echo $latitude?>"; alert(latitude); function initialize() { var mapoptions = { center: new google.maps.latlng(29.6520, -82.3250), zoom:10 }; var map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); } google.maps.event.adddomlistener(window, 'load', initialize); </script> <body> <head><strong>recent locations</strong></head> <div id="map-canvas"/> </body>

in above code not see reason why latitude isn't beingness alerted user. have made few changes think should implement prevent xss , error checking. should check if $_post variables exist before using them. reason why it's returning empty string.

<?php function latlong_xss_check($value) { $value = strip_tags($value); if(!is_numeric($value)) { homecoming 0; } homecoming $value; } if(isset($_post['latitude'],$_post['longitude'])) { $latitude = latlong_xss_check($_post['latitude']); $longitude = latlong_xss_check($_post['longitude']); }else{ $latitude = 0; $longitude = 0; } ?> <script> var latitude = "<?php echo $latitude; ?>"; var longitude = "<?php echo $longitude; ?>"; alert(latitude + ' : ' + longitude); </script>

javascript php post

No comments:

Post a Comment