Jump to content

php/mysql form help


Recommended Posts


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

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 text 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 someone plz have a look at the code below help me.

if anyone think of a better to way than plz let me know. All I want is when user enters the postcode/ id the full address shows (saved in mysql) in a textbox to make sure that the correct post code is entered.

I posted the same problem in a newbie section aswell so sorry if you get see the same problem again.
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 = ''; // Set this to your MySQL username
$database_password = ''; // 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.