Hi all,

I mistakingly posted this in the PHP forum but hope to find my solution here. I am trying to make a dropdown system for states and cities but the cities aren't coming up correctly.

I have this on the root page:

<?     
     echo "<form name=sel>\n";
     echo "States : <font id=states><select>\n";
     echo "<option value='0'>============</option> \n" ;
     echo "</select></font>\n";
     
     echo "Cities : <font id=cities><select>\n";
     echo "<option value='0'>=== none ===</option> \n" ;
     echo "</select></font>\n";
?>

<script language=Javascript>
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //retuen value
               } 
          }
     };
     req.open("GET", "state.php?data="+src+"&val="+val); //make connection
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
     req.send(null); //send value
}

window.onLoad=dochange('states', -1);         // value in first dropdown
</script>

And calling on this script:

<?
    
	 
       //set database
define("_VALID_PHP", true);
  require_once("../include/config.php"); 
  
  
  if (isset($_POST['login']))
      : $result = $session->login($_POST['username'], $_POST['password'], isset($_POST['remember']));
  // Login successful 
  if ($result && $session->userlevel == '2')
      : redirect("useredit.php");
    elseif ($result && $session->userlevel == '1')
      : redirect("agentedit.php");
    elseif ($result && $session->userlevel == '9')
      : redirect("admin/index.php");
	  else : redirect("index.php");
  
  endif;
  endif;


     
     
     $data=$_GET['data'];
     $val=$_GET['val'];
     
     switch($data){
    case "states": // $data == "states"
        echo "<select name='state' onChange=\"dochange('cities', this.value)\">\n";
        echo "<option value='0'>==== Choose State ====</option>\n";
        $sql = "SELECT id, state FROM users ORDER BY state";
		$result = $db->query($sql);
        while ($row = $db->fetch($result)) {
			$state = $row['state'];
            echo "<option value=\"". $state . "\">" . $state . "</option> \n";
        }
    break;
    case "cities": // $data == "cities"
        echo "<select name='cities'>\n";
        echo "<option value='0'>==== Choose City ====</option>\n";
        $sql = "SELECT id, city FROM users WHERE id = '$val' ORDER BY city ";
		$result = $db->query($sql);
        while ($row = $db->fetch($result)) {
			$city = $row['city'];
            echo "<option value=\"". $city . "\">" . $city . "</option> \n";
        }
    break;
}
echo "</select>\n";
?>

Really appreciate any help on this and look forward to your answer.

Cheers...

Member Avatar for P0lT10n

Again you :) hello ;)

I see that you never do this: name=lalala

You MUST do this: name="lala" or name='lala' but CLOSE WITH "" OR '' YOUR ATTRIBUTES...

Dont do <form name=sel> you MUST write this <form name="sel"> and with all...

The same for document.getElementById(src).innerHTML, you MUST do this: document.getElementById("src").innerHTML.

And why Null ??? req.send(null);

do req.send();

Hey POIT10n,

Apologies for the late response... Been hectic lately.

All seems to be working quite good now. There is one situation I am dealing with. It shows a state several different times so I will need an INNER JOIN. Trouble is I am not certain how to write this and get the results I need. Would really appreciate some clarification on this one.

Cheers....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.