Jump to content

Recommended Posts

I want to show one select box in which selecting any option would show another select box of sub fields here's the code.

<html>
  <head>
  <title>Drop down list usage in AJAX</title>
  <script type="text/javascript">
  function show_second(choice){
  var xmlhttp;
  if(window.XMLHttpRequest){
    //for firefox
    xmlhttp = new XMLHttpRequest();
    }
  else{
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
         {
          document.getElementById("first").innerHtml = xmlhttp.responseText;
         }
      } 
      xmlhttp.open("GET","getSecond.php?q="+choice,true);
      xmlhttp.send();
    
  }
  </script>
  </head>
  <body>
   <form>
   <select name ="first" onchange=show_second(this.value)>
   <option value="karachi">Karachi</option>
   <option value="lahore">Lahore</option>
   <option value="islamabad">Islamabad</option> 
   </select> 
   <p id="second">  </p>
   </form>
  </body>
</html>

php code is as follows

<?php
$arrKarachi = array("Nazimabad","Gulshan","F.BArea");
$arrIslamabad = array("Defence","kinhgt");
$arrLahore = array("Abc","Def","Ghi");
  $val = $_GET["q"];
  echo "$val";
  switch($val){
    case 'karachi':
                  {
                  echo "<select name = 'second'>";
                  $cnt = count($arrKarachi);
                  for($i=0;$i<$cnt;$i++){
                  echo "<option value='".$arrKarachi[$i]."'>".$arrKarachi[$i]."</option>";
                  }
                  echo "</select>";
                  break;
                  }
    case 'lahore' :
                  {
                  echo "<select name='second'>";
                  $cnt = count($arrLahore);
                  for($i=0;$i<$cnt;$i++){
                  echo "<option value='".$arrLahore[$i]."'>".$arrLahore[$i]."</option>";
                  }
                  echo "</select>";
                  break;
                  }
    
    case 'islamabad':
                  {
                  echo "<select name='second'>";
                  $cnt = count($arrIslamabad);
                  for($i=0;$i<$cnt;$i++){
                  echo "<option value='".$arrIslamabad[$i]."'>".$arrIslamabad[$i]."</option>";
                  }
                  echo "</select>";
                  break;
                 }
  } 
  //case
?>

Link to comment
https://forums.phpfreaks.com/topic/233567-code-not-showing-correct-results/
Share on other sites

Could not get your ajax to work so substituted mine - I also added a first option so that onchange would work

NEW HTML CODE

<html>
  <head>
  <title>Drop down list usage in AJAX</title>
<script type="text/javascript">
// Placed in <body> so it exicutes while page loads
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
        httpRequest.overrideMimeType('text/xml');
    }
}
else if (window.ActiveXObject) { // IE
    try {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
    }
}
window.jaxobject =	httpRequest; // makes a universal var

function show_second(choice) {
var httpRequest = window.jaxobject;
if (!httpRequest) {
    alert('Giving up  Cannot create an XMLHTTP instance');
    return false;
}
/*url = "test.php?q="+choice;*/
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
httpRequest.open('GET', "test.php?q="+choice, true);
httpRequest.send('');
}

function alertContents(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
		document.getElementById('second').innerHTML = httpRequest.responseText;
        } else {
            alert('There was a problem with the request. '+ httpRequest.status);

        }
    }
}
</script>
  </head>
  <body>
   <form>
   <select name ="first" onchange=show_second(this.value)>
   <option value="">Make A Choice</option>
   <option value="karachi">Karachi</option>
   <option value="lahore">Lahore</option>
   <option value="islamabad">Islamabad</option>
   </select>
   <p id="second">  </p>
   </form>
  </body>
</html>

 

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.