I have an issue that may or may not have been solved before, but I seem to be the only one on here using pure JavaScript instead of JQuery to accomplish my simple AJAX requests.
First here is my AJAX:
function getZestimate(address,csz){
var xmlhttp = new XMLHttpRequest();
var userdata = "address="+address+"&csz="+csz;
xmlhttp.open("POST","../wp-content/themes/realhomes/submit_address.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
retrieve = JSON.parse(xmlhttp.responseText);
document.getElementById("zestimateArea").innerHTML =
'<div id="zillowWrap">
<div id="logoANDtag">
<a href="http://www.zillow.com"><img src="http://www.zillow.com/widgets/GetVersionedResource.htm?path=/static/logos/Zillowlogo_150x40.gif" width="150" height="40" alt="Zillow Real Estate Search" id="ZillowLogo" /></a>
<span id="zestimateTag">Zestimate®</span>
</div>
<span id="zestimatePrice">'+retrieve[0]+'</span>
</div>
<div id="zillowDisclaimer">
<span>© Zillow, Inc., 2006-2014. Use is subject to <a href="http://www.zillow.com/corp/Terms.htm">Terms of Use</a></span
<span>What’s a <a href="http://www.zillow.com/wikipages/What-is-a-Zestimate">Zestimate?</a>
</div>';
}
else{
document.getElementById("zestimateArea").innerHTML = "Error!"
}
}
xmlhttp.send(userdata);
document.getElementById("zestimateArea").innerHTML = "Generating...";
return false;
}
Next, here is my PHP:
<?php
$zillow_id = '1234';
$search = $_POST['address'];
$citystate = $_POST['csz'];
$address = urlencode($search);
$citystatezip = urlencode($citystate);
$url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=".$zillow_id."&address=".$address."&citystatezip=".$citystatezip;
$result = file_get_contents($url);
$data = simplexml_load_string($result);
$zpidNum = $data->response->results->result[0]->zpid;
$zurl = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=".$zillow_id."&zpid=".$zpidNum;
$zresult = file_get_contents($zurl);
$zdata = simplexml_load_string($zresult);
$zestimate=$zdata->response->zestimate->amount;
$street=$zdata->response->address->street;
$city=$zdata->response->address->city;
$state=$zdata->response->address->state;
$zip=$zdata->response->address->zip;
$one='one';
$two='two';
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode(array($zestimate,$street));
?>
What returns in my AJAX is [object Object]
with no errors in my Console.
However, see the 2 variables $one
and $two
? If I place them in the json_encode
like echo json_encode(array($one,$two));
it returns one
like it is supposed to.
I am not sure what the difference is with the Zillow data. I can echo
it individually no problem. But I need to send multiple values to work with. Any ideas?
When you parse a document using SimpleXML, all the nodes are objects which get cast to strings when you try to echo them, but when given to a function like json_encode
, you don't get the results you'd expect.
To make them strings so json_encode
works, try this:
$zestimate = (string)$zdata->response->zestimate->amount;
$street = (string)$zdata->response->address->street;
echo json_encode([$zestimate, $street]);