Jump to content

Correct my code please.


Recommended Posts

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

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.