programming.name Posted April 4, 2010 Share Posted April 4, 2010 Hi, I am writing an application to retrieve data from a database and I've got some minor problem. Please see the following code. a.php ---------------------------------------------------------------------------- <?php function db_connect() { $database_name = 'mysql'; // Set this to your Database Name $database_username = 'root'; // 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) { $zipcode = $_GET['param']; // The parameter passed to us $query = "SELECT * FROM zipcodes WHERE zipcode = '$zipcode'"; $result = mysql_query($query,$conn); $count = mysql_num_rows($result); if ($count > 0) { $name = mysql_result($result,0,'city'); $state = mysql_result($result,0,'state'); } } if (isset($name) && isset($state)) { $return_value = $name . "," . $state; } else { $return_value = "invalid".",".$_GET['param']; // Include Zip for debugging purposes } echo $return_value; // This will become the response value for the XMLHttpRequest object ?> a.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></title> <script language="javascript" type="text/javascript"> var url = "a.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 results = http.responseText.split(","); document.getElementById('name').value = results[0]; isWorking = false; } } } var isWorking = false; function updateCityState() { if (!isWorking && http) { var zipValue = document.getElementById("zip").value; http.open("GET", url + escape(zipValue), true); http.onreadystatechange = handleHttpResponse; isWorking = true; http.send(null); } } function getHTTPObject() { var xmlhttp; 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="post"> <p> ZIP code: <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" /> </p> City: <input type="text" name="name" id="name" /> <input type="text" name="state" id="state" /> </form> </body> </html> All it does is that if I enter the zip code in the form field, it displays the city and state. Basically it works just fine, but I want to modify tow things which might be more advanced. The problem is that the data from the database will only echo the result when I onblur the field, but the way I want that as soon as I finish entering the zip code it should echo the result immediately. Also I want to display the city only, not state. I tried to modify the files by deleting the state field in the form, and changed a bit of code here and there, but, without solid understanding, I had no idea exactly where to change. Please Help me. Thanks Quote Link to comment Share on other sites More sharing options...
programming.name Posted April 5, 2010 Author Share Posted April 5, 2010 I figued out by myself. Thanks anyway! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.