hheyh222
12-16-2010, 03:03 PM
Hello,
Im currently working on a form where a person selects from a drop down list, a state, and then city options become available in another drop down list.
I have that part working perfectly, but Im not sure how I would then make it so when a city is selected and the submit button is pushed, it will go to a link corresponding with the city.
Heres my javascript/html
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function fillSelect(country) {
var url = "http://****************/city.php?country=" + escape(country);
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange =go;
xmlhttp.send(null);
}
function go() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var response = xmlhttp.responseText;
var list=document.getElementById("City");
var cities=response.split('|');
for (i=1; i<cities.length; i++) {
var x=document.createElement('option');
var y=document.createTextNode(cities[i]);
x.appendChild(y);
list.appendChild(x);
}
}
}
}
function display()
{
var country=document.getElementById('country');
country.onchange=function() {
if(this.value!="") {
var list=document.getElementById('City');
while (list.childNodes[0]) {
list.removeChild(list.childNodes[0])
}
fillSelect(this.value);
}
}
fillSelect(country.value)
}
</script>
<body onload="display()">
<center><form name="select" id="select">
<select name="country" id="country" >
<option selected="selected">Select State</option>
<option>Michigan</option>
</select>
<select name="City" id="City" >
<option>Select City</option>
</select>
</form><br /><br />
<input name="submit" type="submit" value="Submit" />
and heres my php
<!-- Script by hscripts.com -->
<?php
echo("ada");
$country=$_GET['country'];
echo($country);
function doIt($country) {
switch ($country) {
case "Michigan":
return array('Traverse City');
break;
}
}
$cities=doIt($country);
foreach ($cities as $city)
{
echo '|'.$city;
}
?>
Thanks a lot for your help
Im currently working on a form where a person selects from a drop down list, a state, and then city options become available in another drop down list.
I have that part working perfectly, but Im not sure how I would then make it so when a city is selected and the submit button is pushed, it will go to a link corresponding with the city.
Heres my javascript/html
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
function fillSelect(country) {
var url = "http://****************/city.php?country=" + escape(country);
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange =go;
xmlhttp.send(null);
}
function go() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var response = xmlhttp.responseText;
var list=document.getElementById("City");
var cities=response.split('|');
for (i=1; i<cities.length; i++) {
var x=document.createElement('option');
var y=document.createTextNode(cities[i]);
x.appendChild(y);
list.appendChild(x);
}
}
}
}
function display()
{
var country=document.getElementById('country');
country.onchange=function() {
if(this.value!="") {
var list=document.getElementById('City');
while (list.childNodes[0]) {
list.removeChild(list.childNodes[0])
}
fillSelect(this.value);
}
}
fillSelect(country.value)
}
</script>
<body onload="display()">
<center><form name="select" id="select">
<select name="country" id="country" >
<option selected="selected">Select State</option>
<option>Michigan</option>
</select>
<select name="City" id="City" >
<option>Select City</option>
</select>
</form><br /><br />
<input name="submit" type="submit" value="Submit" />
and heres my php
<!-- Script by hscripts.com -->
<?php
echo("ada");
$country=$_GET['country'];
echo($country);
function doIt($country) {
switch ($country) {
case "Michigan":
return array('Traverse City');
break;
}
}
$cities=doIt($country);
foreach ($cities as $city)
{
echo '|'.$city;
}
?>
Thanks a lot for your help