Jump to content

populate city textfield with zipcode


stijn0713

Recommended Posts

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

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 ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.