Jump to content

ajax with php for DYNAMIC DROP DOWN LIST PROBLEM??????


rajmohan

Recommended Posts

here is the file called HHAajaxResponse.php

<?php

$dbhost = "localhost";

$dbuser = "root";

$dbpass = "";

$dbname = "testing";

//Connect to MySQL Server

mysql_connect($dbhost, $dbuser, $dbpass);

 

//Select Database

mysql_select_db($dbname) or die(mysql_error());

 

// Retrieve data from Query String

$subregion = $_GET['subregion'];

 

// Escape User Input to help prevent SQL Injection

$subregion = mysql_real_escape_string($subregion);

 

//build query

$query = "SELECT * FROM suburbs WHERE city_id = '$subregion'";

 

//Execute query

$qry_result = mysql_query($query) or die(mysql_error());

 

// Insert a new row in the table for each person returned

while($row = mysql_fetch_array($qry_result)){

 

$display_string .= "<option value=".$row[suburb_id].">".$row[suburb_name]."</option>";

//$display_string .= "<option value=\"$row[suburb_id]\">$row[suburb_name]</option>";

 

}

 

echo "$display_string";

?>

 

 

 

 

 

and html file as index.php

 

 

 

 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>ajaxForm</title>

 

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

<!--

//Browser Support Code

function ajaxFunction(){

 

var ajaxRequest; // The variable that makes Ajax possible!

 

try{

// Opera 8.0+, Firefox, Safari

ajaxRequest = new XMLHttpRequest();

} catch (e){

// Internet Explorer Browsers

try{

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

} catch (e) {

try{

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

} catch (e){

// Something went wrong

alert("Your browser broke!");

return false;

}

}

}

// Create a function that will receive data sent from the server

ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){

var ajaxDisplay = document.getElementById('suburbs');

ajaxDisplay.innerHTML = ajaxRequest.responseText;

}

}

var subregion = document.getElementById('subregion').value;

var queryString = "?subregion=" + subregion;

alert(queryString);

ajaxRequest.open("GET", "HHAajaxResponse.php" + queryString, true);

ajaxRequest.send(null);

alert

}

 

function syncbox() {

sexlist = document.myForm.sex.value;

document.myForm.sexupdate.value = sexlist;

}

function sync(){

interval = setInterval("syncbox()",1);

}

//-->

</script>

 

</head>

 

<body>

<form id="form1" name="form1" method="post" action="">

<label>area

<select name="select" id="subregion" onchange="ajaxFunction()">

<option value="0">select</option>

<option value="1">durban</option>

<option value="2">joburg</option>

<option value="3">capetown</option>

</select>

</label>

<label></label>

<p>

 

<label>sub area</label>

<select name="suburbs" id="suburbs">

</select>

</p>

</form>

 

 

</body>

</html>

 

my table

 

CREATE TABLE `suburbs` (

  `city_id` int(11) NOT NULL,

  `suburb_id` int(11) NOT NULL,

  `suburb_name` varchar(30) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

#

# Dumping data for table `suburbs`

#

 

INSERT INTO `suburbs` VALUES (1, 1, 'one');

INSERT INTO `suburbs` VALUES (1, 2, 'two');

INSERT INTO `suburbs` VALUES (2, 1, 'ha');

INSERT INTO `suburbs` VALUES (2, 2, 'va');

INSERT INTO `suburbs` VALUES (3, 1, 'asd');

INSERT INTO `suburbs` VALUES (3, 2, 'asdf');

   

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.