xampp - How to set selected value for dependent dropdown in php -


i have staff master form, in have 2 dropdowns 1)department 2)designation.now facing problem, when click edit button department dropdown load selected value designation not loading value w.r.t selected department.here code.

staff master form code :

<?php  require "ajax.php";       $deptid=0;       $desgnid=0;       $name="";       $empid="";       $emailid="";       $username="";       $password="";       $mobileno="";       $id=0;       $edit_state= false;    //to connect database  $db = mysqli_connect('localhost', 'root', '1234', 'employee');   //to load department dropdown  $dresult=mysqli_query($db, "select * department");    if(isset($_post['save']))   {       $deptid=$_post['dept'];       //echo $deptid . '<br>';       $desgnid=$_post['designationid'];       //echo 'desgn : '.$desgnid . '<br>';       $name=$_post['name'];       //echo $name . '<br>';       $empid=$_post['employeeid'];       //echo $empid . '<br>';       $emailid=$_post['emailid'];       //echo $emailid . '<br>';       $username=$_post['username'];       //echo $username . '<br>';       $password=$_post['password'];       //echo $password . '<br>';       $mobileno=$_post['mobileno'];       //echo $mobileno . '<br><br><br><br>';        $insertqry="insert employee_details (staff_name, employee_id, email_id, username, password, mobile_no, department_id, designation_id) values                                                       ('$name','$empid','$emailid', '$username', '$password', '$mobileno', '$deptid', '$desgnid')";        //echo $insertqry . '<br>';       mysqli_query($db, $insertqry);          $_session['msg']="data saved successfully";       $deptid=0;       $desgnid=0;       $name="";       $empid="";       $emailid="";       $username="";       $password="";       $mobileno="";    }    if(isset($_post['update']))   {       $deptid=mysql_real_escape_string($_post['dept']);       $desgnid=mysql_real_escape_string($_post['designationid']);       //echo 'desgn : '.$desgnid . '<br>';       $name=mysql_real_escape_string($_post['name']);      // echo $name . '<br>';       $empid=mysql_real_escape_string($_post['employeeid']);       //echo $empid . '<br>';       $emailid=mysql_real_escape_string($_post['emailid']);       //echo $emailid . '<br>';       $mobileno=mysql_real_escape_string($_post['mobileno']);       $id=mysql_real_escape_string($_post['id']);       // echo $id . '<br>';        $updateqry="update employee_details set staff_name='$name', employee_id='$empid', email_id='$emailid', mobile_no='$mobileno', department_id='$deptid', designation_id='$desgnid' staff_id=$id";       // echo $updateqry . '<br>';       mysqli_query($db, $updateqry);       $_session['msg']="data updated successfully";        $deptid=0;       $desgnid=0;       $name="";       $empid="";       $emailid="";       $username="";       $password="";       $mobileno="";       $id=0;   }    //delete records if(isset($_get['delete'])) {     $id= $_get['delete'];      $delete=mysqli_query($db, "delete employee_details staff_id=$id");     $_session['msg']="deleted successfully";     $id=0; }  //to staff details   $staffdetailsresult=mysqli_query($db, "select * employee_details");     // if click on edit fetch data controls              if(isset($_get['edit']))              {                  $id=$_get['edit'];                  $edit_state=true;                  $fetchresult=mysqli_query($db, "select * employee_details staff_id=$id");                   $record=mysqli_fetch_array($fetchresult);                  $deptid=$record['department_id'];                   $desgnid =$record['designation_id'];                  ?>                   <script type="text/javascript">                      //to load desingation on department                        function change_country_edit()                        {                                                          var xmlhttp=new xmlhttprequest();                            xmlhttp.open("get", "staff_edit.php?dptid="+"<?php echo $deptid  ?>"+"&desid="+"<?php echo $desgnid ?>" , false);                            xmlhttp.send(null);                            alert(xmlhttp.responsetext);                            document.getelementbyid("desgn").innerhtml=xmlhttp.responsetext;                        }               </script>                  <?php                  echo '<script> change_country_edit(); </script>';                   $name=$record['staff_name'];                  $empid=$record['employee_id'];                  $emailid=$record['email_id'];                  $username=$record['username'];                  $mobileno=$record['mobile_no'];               } ?> <!doctype html> <html>  <head>   <title>staff master</title>   <link rel="stylesheet" type="text/css" href="style.css">     <script type="text/javascript">          //to load desingation on department        function change_country()        {            var xmlhttp=new xmlhttprequest();            xmlhttp.open("get", "ajax.php?dptid="+document.getelementbyid("dept").value , false);            xmlhttp.send(null);            document.getelementbyid("desgn").innerhtml=xmlhttp.responsetext;        }               // designation id , store in hidden field        function changehiddeninput(objdropdown)        {            var objhidden = document.getelementbyid("designationid");            //alert(objdropdown.value);            objhidden.value = objdropdown.value;        }        </script>  </head>  <body style="background-color:#000000 "> <?php      if(isset($_session['msg'])): ?>         <div class="msg">             <?php                 echo $_session['msg'];                 unset($_session['msg']);              ?>         </div>      <?php endif ?>        <table width="100%" style="color: #ffffff">           <tr>               <td class="heading">staff master</td>           </tr>           <tr>               <td>               <table width="100%">                   <tr>                       <td class="form1" valign="top">                       <form action="staff_master.php" method="post">                             <input type="hidden" name="id"  value="<?php echo $id; ?>"/>                              <input type="hidden" id="designationid" name="designationid" />                              <div class="input-group">                                 <div class="select-style">                                     <select name="dept" id="dept" onchange="change_country()">                                         <option value="-1">--select department--</option>                                           <?php                                              while($deptrow=mysqli_fetch_array($dresult))                                              {   ?>                                                  <option value="<?php echo $deptrow['id']; ?>" <?php if ($deptrow['id'] == $deptid) echo "selected='selected'";?>><?php echo $deptrow['name']; ?></option>                                                  <?php                                              }                                            ?>                                     </select>                                 </div>                                 <div class="input-group">                                         <div class="select-style">                                             <select name="desgn" id="desgn" onchange="changehiddeninput(this)">                                                 <option value="-1"  >--select designation--</option>                                             </select>                                         </div>                                 </div>                             <div class="input-group">                                 <input type="text" name="name" placeholder="staff name" value="<?php echo $name ?>"/>                             </div>                             <div class="input-group">                                 <input type="text" name="employeeid" placeholder="employee id" value="<?php echo $empid ?>"/>                             </div>                              <div class="input-group">                                 <input type="text" name="emailid"  placeholder="email id" value="<?php echo $emailid ?>"/>                             </div>                              <div class="input-group">                                 <input type="text" name="username"  placeholder="user name" value="<?php echo $username ?>"/>                             </div>                              <div class="input-group">                                 <input type="password" name="password"  placeholder="password" value="<?php echo $username ?>"/>                             </div>                             <div class="input-group">                                 <input type="text" name="mobileno"  placeholder="mobile no." value="<?php echo $mobileno ?>"/>                             </div>                             <div class="input-group">                               <?php if($edit_state  == false): ?>                                 <button type="submit" name="save" class="btn">save</button>                               <?php else: ?>                                 <button type="submit" name="update" class="btn">update</button>                               <?php endif ?>                              </div>                         </form>                       </td>                       <td valign="top" align="center" class="form1" style="overflow: auto; height: 420px">                             <table class="table1" align="center" width="100%">                                  <thead>                                      <tr>                                          <th style="text-align: center;">name</th>                                          <th style="text-align: center;">employee id</th>                                          <th style="text-align: center;">email id</th>                                          <th style="text-align: center;">user name</th>                                          <th style="text-align: center;">mobile no</th>                                          <th style="text-align: center;">edit</th>                                          <th style="text-align: center;">delete</th>                                      </tr>                                  </thead>                                  <tbody>                                     <?php                                       while($row = mysqli_fetch_array($staffdetailsresult))                                      {    ?>                                      <tr>                                          <td style="width: 100px; "><?php echo $row['staff_name']; ?></td>                                          <td style="width: 100px;"><?php echo $row['employee_id']; ?></td>                                           <td style="width: 100px;"><?php echo $row['email_id']; ?></td>                                           <td style="width: 100px;"><?php echo $row['username']; ?></td>                                           <td style="width: 100px;"><?php echo $row['mobile_no']; ?></td>                                          <td style="width: 100px; text-align: center;">                                             <a href="staff_master.php?edit=<?php echo $row['staff_id']; ?>" style="color: #ffffff">edit</a>                                          </td>                                          <td style="width: 100px; text-align: center;">                                             <a href="staff_master.php?delete=<?php echo $row['staff_id']; ?>" style="color: #ffffff">delete</a>                                          </td>                                      </tr>                                      <?php                                      }                                        ?>                                   </tbody>                             </table>                       </td>                   </tr>               </table>               </td>           </tr>       </table>     </body> </html> 

to load designation based department have used script , redirecting ajax.php. here code

<?php $db = mysqli_connect('localhost', 'root', '1234', 'employee');   if(isset($_get['dptid']))  {      $deptid=$_get['dptid'];       $dsgnresult=mysqli_query($db, "select * designation dept_id=$deptid");      echo "<select name='desgn' id='desgn'>";      echo "<option value='-1'>";               echo "--select designation--";            echo "</option>";        while($desgnrow=mysqli_fetch_array($dsgnresult))          {            echo "<option value='"; echo $desgnrow['id']; echo "'>";               echo $desgnrow['name'];            echo "</option>";          }       echo "</select>";   }  ?> 

then when click on edit button. calling same javascript little changes below code.

function change_country_edit()                            {                                //alert('in edit');                                //alert('dept : '+<?php echo $deptid  ?>);                                 //alert('desi : '+<?php echo $desgnid  ?>);                                var xmlhttp=new xmlhttprequest();                                xmlhttp.open("get", "staff_edit.php?dptid="+"<?php echo $deptid  ?>"+"&desid="+"<?php echo $desgnid ?>" , false);                                xmlhttp.send(null);                                alert(xmlhttp.responsetext);                                document.getelementbyid("desgn").innerhtml=xmlhttp.responsetext;                            }                   </script> 

staff_edit.php used load designation selected value after clicking edit button. , code below :

<?php $db = mysqli_connect('localhost', 'root', '1234', 'employee');       if(isset($_get['dptid']) && isset($_get['desid']))      {          // echo "in first condition";          $deptid=$_get['dptid'];          // echo  'dept : '.$deptid.'<br>';           $desgnid=$_get['desid'];         //  echo  '$desgnid : '.$desgnid.'<br>';           $dsgnresult=mysqli_query($db, "select * designation dept_id=$deptid");      } ?>   <!doctype html>  <html>  <head>   <title>hello!</title> </head> <body>  <select name="desgn" id="desgn" >     <option value="-1">--select--</option>       <?php          while($deptrow=mysqli_fetch_array($dsgnresult))          {   ?>              <option value="<?php echo $deptrow['id']; ?>" ><?php echo $deptrow['name']; ?></option>              <?php          }        ?> </select> </body> </html> 

this above code have used.searched many things , applied of no use. please me out of problem :( .

thank in advanced.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -