title: Validate Dynamic Dropdown With Ajax
body: <p>I have a drop down menu of mobile brands. When a user selects a mobile brand a dynamic drop populates with the mobile model according to the mobile brand I want to validate that mobile model drop down for a blank select value. I am using ajax xmlhttp request for getting the mobile model drop down menu. </p>
  
  <p>My entire code is:</p>
  
  <p>Code for page with mobile brand drop down menu</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function validate() {
          if (document.myform.step3_mobilebrand.value=="") {
              document.getElementById("error1").innerHTML = (error1) ? "&lt;img src='images/formerror.gif' height='15' width='18'&gt;" : "";
              document.myform.step3_mobilebrand.focus();
              return false;
          }
  
          var myTextField = document.myform.getElementById('step3_mobilemodel').value;
  
          if (myTextField == "") {
              document.getElementById("error2").innerHTML = (error2)?"&lt;img src='images/formerror.gif' height='15' width='18'&gt;" : "";
              document.myform.step3_mobilemodel.focus();
              return false;
          }
  
  
      }
  &lt;/script&gt;
  
  &lt;?php
  if (isset($_POST['submitted']) and $_POST['submitted']=="Continue") {
      $mobilebrand1 = $_POST['step3_mobilebrand'];
      $mobilemodel1 = $_POST['step3_mobilemodel'];
  
      if ($mobilemodel1=="") {
          echo "Please Select Mobile Model";          
      }
  
      $connectiontype=$_POST['step3_connectiontype'];
      $internet=$_POST['step3_internet'];
  
      include("admin/config.php");
  
      $sql="insert into member_detail (mobilebrand,mobilemodel,connection_type,internet) values('$mobilebrand1','$mobilemodel1','$connectiontype','$internet')";
  
      mysql_query($sql);
  }
  ?&gt;
  
  &lt;script type="text/javascript"&gt;
  function showUser(str) {
      if (str=="") {
          document.getElementById("txtHint").innerHTML="";
          return;
      } 
  
      if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      } else {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
  
      xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
              document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
          }
      }
  
      xmlhttp.open("GET","getuser.php?q="+str,true);
      xmlhttp.send();
  }
  &lt;/script&gt;
  
  &lt;form name="myform" method="post" action="" onsubmit="return validate();"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Mobile Brand&lt;/td&gt;
              &lt;td&gt;&lt;?php 
                  include ("admin/config.php");
                  echo "&lt;select name='step3_mobilebrand' onchange='showUser(this.value)'&gt;&lt;option value='' selected&gt;Select Mobile Brand&lt;/option&gt;";
                  $result = mysql_query ("select * from mobilebrand order by mobilebrand");
                  while ($adcom=mysql_fetch_array($result)) {
                      echo "&lt;option value=\"".$adcom['id']."\"&gt;".htmlspecialchars($adcom['mobilebrand'])."&lt;/option&gt;";
                  }
  
                  echo "&lt;/select&gt;";?&gt;&lt;span id="error1" style="color:red;"&gt;&lt;/span&gt;&lt;/td&gt;
  
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;div id="txtHint"&gt;&lt;/div&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Connection Type&lt;/td&gt;
              &lt;td&gt;&lt;input type="radio" name="step3_connectiontype" value="PrePaid" checked="checked"&gt;Prepaid
                  &lt;input type="radio" name="step3_connectiontype" value="PostPaid"&gt;Postpaid
                  &lt;span id="error3" style="color:red;"&gt;&lt;/span&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Have Internet On Mobile&lt;/td&gt;
              &lt;td&gt;&lt;input type="radio" name="step3_internet" value="Yes" checked="checked"&gt;Yes
                  &lt;input type="radio" name="step3_internet" value="No"&gt;No
                  &lt;span id="error4" style="color:red;"&gt;&lt;/span&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="submit" name="submitted" value="Continue"&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and the code of getuser.php is</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;Mobile Model&lt;/td&gt;
          &lt;td&gt;
              &lt;?php
              $q=$_GET["q"];
              include("admin/config.php");
                echo "&lt;select name='step3_mobilemodel' id='step3_mobilemodel' &gt;&lt;option value=''&gt;Select Mobile Model&lt;/option&gt;";
              $result = mysql_query ("select * from mobilemodel where brandcode='".$q."'");
              while ($adcom=mysql_fetch_array($result)) {
                  echo "&lt;option value=\"".$adcom['mobilemodel']."\"&gt;".htmlspecialchars($adcom['mobilemodel'])."&lt;/option&gt;";
              }
                  echo "&lt;/select&gt;"; ?&gt;
  
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>The Output am getting is correct i just want to validate the mobile model drop down menu for blank select </p>