Jump to content

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


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');

   

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.