stijn0713 Posted April 11, 2012 Share Posted April 11, 2012 I have a problem with the following code but i really don't see it: I try to populate the city textfield by the zipcode entered on the zipcode textfield function fillcitystate(controlname) { var zipstring = ""; xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "postcode.php?postcode=" + controlname.value, true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { var zipstring = xmlhttp.responseText; if (zipstring!="Error") { var ziparray = zipstring.split("|"); document.getElementById("test").innerHTML = ziparray[1]; } } } xmlhttp.send(null); } In postcode.php $returnval = "Error"; $postcode = $_GET['postcode']; $query = "SELECT * FROM city WHERE code='$postcode'"; $resultval = mysql_query($query); $rowcount = mysql_num_rows($resultval); if ($rowcount==1) { $row = mysql_fetch_array($resultval); $returnval = $row['code']."|".ucwords(strtolower($row['name']))."|".$row['province']; } else { } echo $returnval; When entering a zipcode, nothing happened so i decided to just echo the name of the city using the innerHTML. Still nothing happened, so i commented out all the php in my postcode.php and decided to just do: $postcode = $_GET['postcode']; echo $postcode; then it printed out: 'undefined' Can somebody point me out? Link to comment https://forums.phpfreaks.com/topic/260755-populate-city-textfield-with-zipcode/ Share on other sites More sharing options...
xyph Posted April 11, 2012 Share Posted April 11, 2012 What happens when you call postcode.php?postcode=value in your browser? Link to comment https://forums.phpfreaks.com/topic/260755-populate-city-textfield-with-zipcode/#findComment-1336478 Share on other sites More sharing options...
stijn0713 Posted April 11, 2012 Author Share Posted April 11, 2012 What happens when you call postcode.php?postcode=value in your browser? Yes, your right, i was not getting a value back from the query because $rowcount was bigger than 1 so i didn't do this code: $row = mysql_fetch_array ($resultval); $returnval = $row['code']."|".ucwords(strtolower($row['name']))."|".$row['province']; } My bad, i didn't knew the database lol. Thanks. Anyway, now i have the correct value but how can i display this is the textfield? just by: document.getElementById('textfieldID').value ? Link to comment https://forums.phpfreaks.com/topic/260755-populate-city-textfield-with-zipcode/#findComment-1336483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.