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
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 ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.