rajmohan Posted August 9, 2007 Share Posted August 9, 2007 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> Link to comment https://forums.phpfreaks.com/topic/64033-ajax-with-php-for-dynamic-drop-down-list-problem/ Share on other sites More sharing options...
rajmohan Posted August 9, 2007 Author Share Posted August 9, 2007 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'); Link to comment https://forums.phpfreaks.com/topic/64033-ajax-with-php-for-dynamic-drop-down-list-problem/#findComment-319166 Share on other sites More sharing options...
JJohnsenDK Posted August 9, 2007 Share Posted August 9, 2007 do tell more... i have no clue what your problem is? and where the problem is? Link to comment https://forums.phpfreaks.com/topic/64033-ajax-with-php-for-dynamic-drop-down-list-problem/#findComment-319178 Share on other sites More sharing options...
rajmohan Posted August 9, 2007 Author Share Posted August 9, 2007 if I put the recieved data from the PHP file I recieve all the info but as soon as I put it into a drop-down list the list does not populate. it just shows up blank. Link to comment https://forums.phpfreaks.com/topic/64033-ajax-with-php-for-dynamic-drop-down-list-problem/#findComment-319195 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.