Jump to content

AJAX send request


Spunky
 Share

Recommended Posts

Taking from a tutorial I found here: http://www.w3schools.com/php/php_ajax_database.asp

 

I created the following code:(the code from link example is in function saveAddress)

var geocoder;var map;function initialize() {  geocoder = new google.maps.Geocoder();  var latlng = new google.maps.LatLng(-34.397, 150.644);  var mapOptions = {    zoom: 8,    center: latlng  }  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);}function saveAddress(str){	 if (str == "") {        alert('Please enter an Address');        return;     } else {         if (window.XMLHttpRequest) {            // code for IE7+, Firefox, Chrome, Opera, Safari            xmlhttp = new XMLHttpRequest();        } else {            // code for IE6, IE5            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        }        /*xmlhttp.onreadystatechange = function() {            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;            }        }*/		alert(str);        xmlhttp.open("GET","saveAddress.php?location="+str,true);        xmlhttp.send();    }}function codeAddress() {  var address = document.getElementById('address').value;  geocoder.geocode( { 'address': address}, function(results, status) {    if (status == google.maps.GeocoderStatus.OK) {      map.setCenter(results[0].geometry.location);      var marker = new google.maps.Marker({          map: map,          position: results[0].geometry.location,      });	  	  saveLatLng = results[0].geometry.location;	  saveAddress(saveLatLng);	      } else {      alert('Geocode was not successful for the following reason: ' + status);    }  });}google.maps.event.addDomListener(window, 'load', initialize);

I commented out part of the AJAX because I wasn't sure what else to do with it, I didn't need the link inside that if statement so I got rid of it altogether.

 

The real issue however is the php file retrieving the request.

$location = $_GET['location'];

I tried getting rid of intval because I didn't need it converted to an integer. But it still gives me a 0 (I have it being placed into a table on a database). Honestly though I am a little confused on that part of the code in the example on what it is even doing there, how it is retrieved if it is turned into a '0'.

 

The last time someone suggested using error_log() when I had a PHP problem but I couldn't figure out where my server's error log is...so if we need that to debug this I am going to need help figuring out how to find that. :boredom:

Edited by Spunky
Link to comment
Share on other sites

Is the database field of type INT?

 

Try printing out the value instead of adding it to the database to see what you're getting. Use the network tab of the browser's developer tools to see what is being sent and what it being received.

Link to comment
Share on other sites

Is the database field of type INT?

 

Try printing out the value instead of adding it to the database to see what you're getting. Use the network tab of the browser's developer tools to see what is being sent and what it being received.

 

It is an INT yes. Or wait, is that a problem because of the comma between the numbers? Hmm.

 

When I try to use echo after the GET line in PHP, nothing appears on the screen. I'm not sure why.

echo '<p>Location:' . $location . '</p>';
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...