Jump to content

help with autofill data using php/mysql


Recommended Posts

Hi guyz,

I am fairly new to php but I do posses some knowledge of mysql. I have created a form which is filled in by the user and updates the back-end database. I have also created a search engine to search from the database. Everything works well. Now I am trying to add more functionality, I want to create a form where user enter postcode and address filled automatically from the backend database.

could any please guide me the right direction on how to do this, an example would be useful.

thanks :)
Link to comment
Share on other sites

If you want this to happen without a page refresh you'd probably use some [a href=\"http://www.allegrezza.org/articles/ajaxtutorial/\" target=\"_blank\"]ajax[/a] trickery. Otherwise, its just a simple as having the client submit the form containing the postcode, search the db for the address matching the postcode, and display the address in another form.
Link to comment
Share on other sites

AJAX is the answer, but no fear, its wicked easy.

check out this link:
[a href=\"http://www.webpasties.com/xmlHttpRequest/\" target=\"_blank\"]http://www.webpasties.com/xmlHttpRequest/[/a]

its a good tutorial for exactily what you want to do.

EDIT: Oops, the first link I had was for a .NET tutorial. wrong forum. The above is the one I used months ago when i first learned AJAX.
Link to comment
Share on other sites

Thanks Thorpe and Micah1701 for responding quickly and helping me out. :). Now I have one more question if there is a new customer and I want add new details into temprory database with given temporary account number e.g. tmp123. I know that I can auto increment numbers in mysql how can I auto_increment text and numbers like tmp123, tmp124... etc

Thanks in advance.
Link to comment
Share on other sites

[!--quoteo(post=357008:date=Mar 21 2006, 11:42 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 21 2006, 11:42 AM) [snapback]357008[/snapback][/div][div class=\'quotemain\'][!--quotec--]
AJAX is the answer, but no fear, its wicked easy.

check out this link:
[a href=\"http://www.webpasties.com/xmlHttpRequest/\" target=\"_blank\"]http://www.webpasties.com/xmlHttpRequest/[/a]

its a good tutorial for exactily what you want to do.

EDIT: Oops, the first link I had was for a .NET tutorial. wrong forum. The above is the one I used months ago when i first learned AJAX.
[/quote]

I have used the above tutorial using my own database and form but I am having a slight problem. which when I run the script:
"http://localhost/getcitystate.php?param=6"
I get correct results:
<?xml version="1.0" standalone="yes" ?>
- <activity_id>
<full_name>sabheen</full_name>
<account_no>233445</account_no>
</activity_id>

however when I type id= 6 in the form It wont fill in itself but when I double click on boxes I get a list to select the full_name and account_no.

I have checked the form many times and couldnt find any problems. Could you plz have a look at the code help me.

Thanks

Here is the form.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>ZIP Code to City and State using XmlHttpRequest</title>

<script language="javascript" type="text/javascript">

var url = "getCityState.php?param="; // The server-side script

function handleHttpResponse() {

if (http.readyState == 4) {

if (http.responseText.indexOf('invalid') == -1) {

// Split the comma delimited response into an array

// Use the XML DOM to unpack the city and state data

var xmlDocument = http.responseXML;

var full_name = xmlDocument.getElementsByTagName('full_name').item(0).firstChild.data;

var account_no = xmlDocument.getElementsByTagName('account_no').item(0).firstChild.data;

document.getElementById('full_name').value = full_name;

document.getElementById('account_no').value = account_no;

isWorking = false;

}

}

}

var isWorking = false;

function updateCityState() {

if (!isWorking && http) {

var activity_idValue = document.getElementById("activity_id").value;

http.open("GET", url + escape(activity_idValue), true);

http.onreadystatechange = handleHttpResponse;

isWorking = true;

http.send(null);

}

}

function getHTTPObject() {

var xmlhttp;

/*@cc_on

@if (@_jscript_version >= 5)

try {

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

} catch (E) {

xmlhttp = false;

}

}

@else

xmlhttp = false;

@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

try {

xmlhttp = new XMLHttpRequest();

} catch (e) {

xmlhttp = false;

}

}

return xmlhttp;

}

var http = getHTTPObject(); // We create the HTTP Object

</script>

</head>

<body>

<form action="GET">

<p>

ID:

<input type="text" size="5" name="activity_id" id="activity_id" onblur="updateCityState();" />

</p>

Full Name:

<input type="text" name="full_name" id="full_name" />

Account No:

<input type="text" size="2" name="account_no" id="account_no" />

</form>

</body>

</html>

getcitystate.php
<?php
/**
* Connects to the database.
* Return false if connection failed.
* Be sure to change the $database_name. $database_username , and
* $database_password values to reflect your database settings.
*/
function db_connect() {
$database_name = 'rep'; // Set this to your Database Name
$database_username = 'root'; // Set this to your MySQL username
$database_password = 'newpwd'; // Set this to your MySQL password
$result = mysql_pconnect('localhost',$database_username, $database_password);
if (!$result) return false;
if (!mysql_select_db($database_name)) return false;
return $result;
}
$conn = db_connect(); // Connect to database
if ($conn) {
$activity_id = $_GET['param']; // The parameter passed to us
$query = "select * from repactivity where activity_id = '$activity_id'";
$result = mysql_query($query,$conn);
$count = mysql_num_rows($result);
if ($count > 0) {
$full_name = mysql_result($result,0,'full_name');
$account_no = mysql_result($result,0,'account_no');
}
}
if (isset($full_name) && isset($account_no)) {
// $return_value = $full_name . "," . $account_no;
$return_value = '<?xml version="1.0" standalone="yes"?><activity_id><full_name>'.$full_name.'</full_name><account_no>'.$account_no.'</account_no></activity_id>';
}
else {
$return_value = "invalid".",".$_GET['param']; // Include Zip for debugging purposes
}
header('Content-Type: text/xml');
echo $return_value; // This will become the response value for the XMLHttpRequest object
?>
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.