| | |
Reload with drop-down and text fields
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 16
Reputation:
Solved Threads: 0

So far i have a database with all data in it.
I have the 1st text field where the user enters their postcode and then the page reloads populating the drop down menu with the corresponding suburbs.
Next what i want is after a suburb has been selected in the list, the page reloads again and the corresponding city, state and country is displayed in the other 3 text fields.
How can i get the query to execute and fill in the last 3 fields after a suburb in the drop list has been selected???????
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>Postcode and Address Selection Test</title> <SCRIPT language=JavaScript> function reload(form) { var val=form.postcode.value; self.location='dd.php?postcode=' + val ; } </script> </head> <body> <? @$postcode=$_GET['postcode']; // Use this line or below line if register_global is off @$city=$_GET['city']; @$state=$_GET['state']; @$country=$_GET['country']; $city = 'not_complete'; $state = 'not_complete'; $country = 'not_complete'; /////// for suburb drop down list we will check if postcode is entered else we will display all the suburbs///// if(isset($postcode) and strlen($postcode) > 0){ $query=mysql_query("SELECT DISTINCT suburb FROM post2 where postcode=$postcode order by suburb"); //Table called POST2 }else{$query2=mysql_query("SELECT DISTINCT suburb FROM post order by suburb"); } ////////// end of query for second subcategory drop down list box ///////////////////////////////////////////// echo "<form method=post name=f1 action='dd-check.php'>";//Goes to dd-check to check posted variables //Page Reloads after 4 digits are entered///<<<<--------------POSTCODE FIELD-------<<<<<<<<<<<<<< echo "Postcode: <input name='postcode' type=text onkeyup=\"if(this.value.length>3)reload(this.form);else return false;\" value=$postcode>"; echo"<input value='Load Suburbs' type='button' onclick=\"reload(this.form)\" > Click to Load Suburbs for this Postcode."; //////////////////Starting of suburb drop downlist///<<<<--------------SUBURB-------<<<<<<<<<<<<< echo"<br><br>"; echo"Suburb: "; echo "<select name='suburb'><option value=''>Select Suburb</option>"; // onchange=\"reload(this.form)\" while($pop = mysql_fetch_array($query)) { echo "<option value='$pop[suburb]'>$pop[suburb]</option>"; }//else{ //echo "<option value='$pop[suburb]'>$pop[suburb]</option>"; //} echo "</select>"; ////////////////// This will end the suburb drop down list /////////// ///////////////Execute this when suburb is selected////////////////////<<-----NEED TO FIX-------<<<<<< //if(isset($suburb) and strlen($suburb) > 0){ $query_city=mysql_query("SELECT city FROM post2 where postcode=$postcode AND $suburb=$suburb"); //}else{ //$city = 'not_complete1'; //} $query_state=mysql_query("SELECT state FROM post2 where postcode=$postcode AND $suburb=$suburb"); $query_country=mysql_query("SELECT country FROM post2 where postcode=$postcode AND $suburb=$suburb"); echo "<br>"; echo "<br>"; echo "City: <input name='city' type=text value=$city>"; echo "<br>"; echo "<br>"; echo "State: <input name='state' type=text value=$state>"; echo "<br>"; echo "<br>"; echo "Country: <input name='country' type=text value=$country>"; //----------------------------------------------- echo "<br><br><input type=submit value=Submit>"; echo "</form>"; ?> </body> </html>
Thankyou,
Billy
Ok, I wrote the script for getting the values from the database in PHP, but you can probably translate it to ASP. The part we're concerned about is the JavaScript.
I didn't have a database to load data from, so I used arrays instead. As I said before, this is a demo, so you'll have to load the data from the database differently. The XML file will display the same.
A demo page can be found here. I don't guarantee that this will stay up forever.
Here's my finished product:
I didn't have a database to load data from, so I used arrays instead. As I said before, this is a demo, so you'll have to load the data from the database differently. The XML file will display the same.
A demo page can be found here. I don't guarantee that this will stay up forever.
Here's my finished product:
demo.html: HTML Syntax (Toggle Plain Text)
<html> <head> <title>AJAX Location Script</title> <script type="text/javascript" src="ajaxloc.js"> // Script created by KnifeySpooney (http://wurbo.com) // This script is free to use and modify. </script> </head> <body onLoad="getLocation('suburb')" style="text-align:center;"> Suburb: <select name="suburb" onChange="if (this.value != '') getLocation('location', this.value); else clearLocations();"> <option value="">- Choose your suburb -</option> </select> <br/> State: <input type="text" name="state"/> <br/> Country: <input type="text" name="country"/> </body> </html>
locations.php PHP Syntax (Toggle Plain Text)
<?php $suburbs = array('Evergreen Terrace', 'Quahog', 'South Park', 'Raleigh'); $states[array_search('Evergreen Terrace', $suburbs)] = 'Illinois'; $states[array_search('Quahog', $suburbs)] = 'Maryland'; $states[array_search('South Park', $suburbs)] = 'Colorado'; $states[array_search('Raleigh', $suburbs)] = 'North Carolina'; $countries[array_search('Evergreen Terrace', $suburbs)] = 'United States'; $countries[array_search('Quahog', $suburbs)] = 'United States'; $countries[array_search('South Park', $suburbs)] = 'United States'; $countries[array_search('Raleigh', $suburbs)] = 'United States'; $type = $_REQUEST['type']; if ($type) { header("Content-Type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>\n"; echo "<location>\n"; if ($type == 'suburb') { for ($i=0; $i<sizeof($suburbs); $i++) { echo "\t<place type='suburb'>" . $suburbs[$i] . "</place>\n"; } } else if ($type == 'location' && $_REQUEST['loc']) { echo "\t<place type='state'>" . $states[array_search($_REQUEST['loc'], $suburbs)] . "</place>\n"; echo "\t<place type='country'>" . $countries[array_search($_REQUEST['loc'], $suburbs)] . "</place>\n"; } else echo "\t<place>Not all variables specified.</place>\n"; echo "</location>"; } else { header("Content-Type: text/html"); echo "<h1>Error: Please enter a type of location to load.</h1>"; } ?>
ajaxloc.js JavaScript Syntax (Toggle Plain Text)
function getLocation(type, where) { var xmlhttp; if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); else alert("Your browser does not support XMLHTTP!"); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4) { var xmlDoc = xmlhttp.responseXML.documentElement; var loc = xmlDoc.getElementsByTagName("place"); if (type == "suburb") { for (var a=0; a<loc.length; a++) { var locVal = loc[a].childNodes[0].nodeValue; var elm = document.createElement("option"); elm.value = locVal; elm.innerHTML = locVal; document.getElementsByName(type)[0].appendChild(elm); } } else { for (var b=0; b<loc.length; b++) { var id = loc[b].attributes.getNamedItem("type").value; document.getElementsByName(id)[0].value = loc[b].childNodes[0].nodeValue; } } } } xmlhttp.open("GET", "locations.php?type=" + type + "&loc=" + where, true); xmlhttp.send(null); } function clearLocations() { document.getElementsByName("state")[0].value = ""; document.getElementsByName("country")[0].value = ""; }
Last edited by itsjareds; May 18th, 2009 at 9:04 pm.
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Catch Errors
- Next Thread: Auto crop pictures
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column cookies createrange() css cursor dependent disablefirebug dom download dropdown editor element engine error events explorer ext file form forms google gwt gxt hiddenvalue highlightedword html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump libcurl math matrixcaptcha microsoft mimic object onerror onmouseoutdivproblem onreadystatechange parent pdf php player post problem progressbar rated rating regex runtime scroll search security select session shopping size software sql star stars synchronous text textarea unicode validation w3c web website window windowofwords windowsxp wysiwyg xml \n





