Jump to content

Autofill from postcode


adamjblakey

Recommended Posts

Hi,

 

What i am trying to do is when i press the check address button i want it to return the entries and automatically enter them in the relevant text field so e.g. you enter postcode and number and press enter the town and street etc get field in.

 

What i cannot do at the moment though is get these value returned so i can echo them out in the right text field. It only returns all the values as one variable.

 

This is my code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var AdminResponse = "";

function parseResponse(){

	var nMessage = document.getElementById('echoMsg');
	nMessage.style.display = "";
	nMessage.innerHTML = AdminResponse;
}

function registerUser(){

	var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
	AdminRequest.onreadystatechange = function()
		{
	 	 if (AdminRequest.readyState == 4)
			{
	 	 	 if (AdminRequest.status == 200)
				{
		 	 	 AdminResponse = AdminRequest.responseText;
		 	 	 parseResponse();
				}
	 	 	 else 	{
			 	 alert('Error lookup.php File '+ AdminRequest.statusText);
				}
			}
		}
	var nForm = document.forms[0];
	var infoStr = "housenumber=" + nForm['housenumber'].value;
	infoStr += "&postcode=" + nForm['postcode'].value;
	AdminRequest.open("POST", "lookup.php", true);
	AdminRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
	AdminRequest.send(infoStr); 
}

function validate(nForm){

	document.getElementById('echoMsg').style.display = 'none';
	for (i=0; i<nForm.length; i++)
		{
		 if (nForm[i].value == "")
			{
			 alert('Please complete all fields');
			 return false;
			}
		}
	registerUser();
}

</script>

</head>
<body>
	<div id='echoMsg' style='display:none'></div>

	<form action="">
	   <fieldset>
		<legend>Personal Information</legend>
			<label>House No : 
			<input type='text' size='15' name='housenumber' class='formField'></label>
			<label>Postcode:     
			<input type='text' size='15' name='postcode' class='formField'></label>
		<input type='button' name='submit' value="Submit" class='submitBtn' onClick="validate(this.form)">
	   </fieldset>
	</form>
</body>
</html>

 

 

<?php

include ("includes/top.php");

	$postcode = str_replace( ' ', '', $_POST['postcode'] );

	$ch = curl_init("http://postcodeloopup.php?postcode=$postcode");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$value = curl_exec($ch);

	preg_match_all('/<(Street|Locality|Town|TVRegion|Latitude|Longitude)>([^<]+)<\/\\1>/', $value, $matches);

	$street .= $matches[2][0];
	$town .= $matches[2][1];
	$region .= $matches[2][2];
	$latitude .= $matches[2][3];
	$longitude .= $matches[2][4];


	$message = "House Number - " . $_POST['housenumber'] . "<br>";
	$message .= "Postcode - " . $postcode. "<br>";
	$message .= "Street - " . $street. "<br>";
	$message .= "Town - " . $town. "<br>";
	$message .= "Region - " . $region. "<br>";
	$message .= "Latitude - " . $latitude. "<br>";
	$message .= "Longitude - " . $longitude. "<br>";


echo stripslashes($message);
?>

 

Cheers,

Adam

Link to comment
Share on other sites

Thanks for your replys i think i know what i need to do but i don't know how to do it.

 

What i think i have to do is explode the results that are returned but i don't know how to do this.

 

This is what i have done with the results:

 

$message = $_POST['housenumber'] . "^";
	$message .= $postcode. "^";
	$message .= $street. "^";
	$message .= $town. "^";
	$message .= $region. "^";
	$message .= $latitude. "^";
	$message .= $longitude. "^";
echo stripslashes($message);

 

 

And when this is returned to the main page i think i need to explode the results in this javascript function but don't how?

 

function parseResponse(){


	var nMessage = document.getElementById('echoMsg');
	nMessage.style.display = "";
	nMessage.innerHTML = AdminResponse;
}


 

example here: coolmove4u(dot)com/design/posttest.php

 

Can anyone help me what i am supposed to do here?

 

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.