Reputation: 73
I am trying to get a full address by entering the postal code in a textbox in HTML form by press a button, I have two files the first one has the ajax function and the second one has the PHP code. I am not sure if my ajax code sending a request to PHP or not, Can anyone help me please?
here is the ajax file:
<script type="text/javascript">
$(document).ready(function(){
$('.addressbutton').click(function(){
ss= document.getElementById("address").value;
alert(ss);
$.ajax({
url: 'findaddress.php',
type: 'post',
data: ss,
success: function(response){
var replay = response.postal_code;
alert(replay);
document.getElementById('address').innerHTML = response.postal_code;
document.getElementById('address2').innerHTML = response.route;
document.getElementById('address3').innerHTML = response.locality;
document.getElementById('address4').innerHTML = response.postal_town;
document.getElementById('address5').innerHTML = response.administrative_area_level_2;
}
});
return false;
});
});
</script>
and here is the PHP code (findaddress.php)
<?php
header('Content-Type: application/json');
$ss=$_POST['address'];
$postcode = urlencode($ss);
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?
address='.$postcode.'&sensor=false';
$parsedXML = simplexml_load_file($url);
if($parsedXML->status != "OK") {
echo "There has been a problem: " . $parsedXML->status;
}
$myAddress = array();
foreach($parsedXML->result->address_component as $component) {
if (is_array($component->type)) {
$type = (string)$component->type[0];
} else {
$type = (string)$component->type;
}
$myAddress[$type] = (string)$component->long_name;
}
$f1 = $myAddress['postal_code'];
$f2 = $myAddress['route'];
$f3 = $myAddress['locality'] ;
$f4 = $myAddress['postal_town'] ;
$f5 = $myAddress['administrative_area_level_2'] ;
$f6 = $myAddress['country'];
//print_r($myAddress);
$ORegisertation = array(
'postal_code' => $f1,
'route' => $f2,
'locality' => $f3,
'postal_town' => $f4,
'administrative_area_level_2' => $f5,
'country' => $f6
);
$account_json = json_encode($ORegisertation);
echo $account_json;
?>
Upvotes: 1
Views: 1033
Reputation: 2683
HTML
<form name="frmRegistration" id="signup-form" method="post">
<div>
<input type="text" name="address" id="address" class="findaddress" placeholder="Postal code"/>
<input type="button" name="addressbutton" class="addressbutton" value="Find" id="findaddress" />
<input type="text" name="address2" id="address2" class="findaddress" placeholder="Line 1"/>
<input type="text" name="address3" id="address3" class="findaddress" placeholder="Line 2"/>
<input type="text" name="address4" id="address4" class="findaddress" placeholder="Line 3"/>
<input type="text" name="address5" id="address5" class="findaddress" placeholder="Line 4"/>
</div>
</form>
Javascript
$(document).ready(function(){
$('.addressbutton').click(function(){
ss = document.getElementById("address").value;
$.ajax({
url: 'findaddress.php',
type: 'post',
data: {address:ss}, //added an index address here
success: function(response){
var replay = response.postal_code;
//innerHTML is not an attribute of text boxes, so changed it to value
document.getElementById('address').value = response.postal_code;
document.getElementById('address2').value = response.route;
document.getElementById('address3').value = response.locality;
document.getElementById('address4').value = response.postal_town;
document.getElementById('address5').value = response.administrative_area_level_2;
},
error: function(response) {
alert("Error: "+response);
}
});
return false;
}); //added closing brace and bracket
});
added comments in script about changes made.
PHP FILE (findaddress.php)
<?php
header('Content-Type: application/json');
$ss = $_POST['address'];
$postcode = urlencode($ss);
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.$postcode.'&sensor=false';
$parsedXML = simplexml_load_file($url);
if($parsedXML->status != "OK") {
echo "There has been a problem: " . $parsedXML->status;
}
$myAddress = array();
foreach($parsedXML->result->address_component as $component) {
if(is_array($component->type)) $type = (string)$component->type[0];
else $type = (string)$component->type;
$myAddress[$type] = (string)$component->long_name;
}
echo json_encode($myAddress);
die();
?>
taken out irrelevant indexing again, and irrelevant statements.
Upvotes: 1
Reputation: 3322
In this case, you want to make sure to define the type of response from the server. I like to place dataType:'json' in my $.ajax calls. Then in your PHP code, make sure to add a header of type application/json. This will make a difference with some browsers. I like to read the Response Preview with Google Chrome. It will automatically parse the response; especially helpful with debugging.
header('Content-type: application/json');
echo json_encode($account_json);
exit;
Upvotes: 0
Reputation: 787
You are not sending data correctly .. if you want to get value of address in php which is post from ajax do this
data: { address: ss},
//
And Either add dataType:'json'
there in your ajax or use jsonParse(response)
you get a string there at your response you cannot directly use response.postal_code;
Upvotes: 0
Reputation: 73
There is the ajax code with the html form just to have a better idea
<form name="frmRegistration" id="signup-form" method="post">
<div><input type="text" name="address" id="address" class="findaddress" placeholder="Postal code"/>
<input type="button" name="addressbutton" class="addressbutton" value="Find" id="findaddress" onclick="javascript:hello()"/>
<input type="text" name="address2" id="address2" class="findaddress" placeholder="Line 1"/>
<input type="text" name="address3" id="address3" class="findaddress" placeholder="Line 2"/>
<input type="text" name="address4" id="address4" class="findaddress" placeholder="Line 3"/>
<input type="text" name="address5" id="address5" class="findaddress" placeholder="Line 4"/>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.addressbutton').click(function(){
ss = document.getElementById("address").value;
//alert(ss);
$.ajax({
url: 'findaddress.php',
type: 'post',
data: {address:ss},
success: function(response){
var replay = response.postal_code;
alert(replay);
document.getElementById('address').innerHTML = response.postal_code;
document.getElementById('address2').innerHTML = response.route;
document.getElementById('address3').innerHTML = response.locality;
document.getElementById('address4').innerHTML = response.postal_town;
document.getElementById('address5').innerHTML = response.administrative_area_level_2;
}
});
return false;
}); //added closing brace and bracket
});
</script>
</form>
Upvotes: 0