Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3614936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:18:40+00:00 2026-05-18T22:18:40+00:00

Hi All I am working in a PHP project. I am stucked with an

  • 0

Hi All
I am working in a PHP project. I am stucked with an issue related with Ajax.

I have A select box for Company on whose selection department is displayed using ajax, and on selection of department from department select box along with another User group select box I get the list of users.

Now on change of User group I Send request to server using ajax as user group id and department id to fetch the respective users. But when I reselect the department I cannot get the new user list as this department list is generated from ajax and Ajax do not handle Javascript.

Code I Am Working With:

select box to select company

 <select name="companyname" id="companyname" onchange="javascript:dochange(this.value);">
    <option value="-1">-----Select Company-----</option>
          <?php foreach($user_obj->getUserCompanyname() as $key => $value){
        $selected = '';
        if($value['company_id'] == $user_obj->user_company_id){
        $selected = 'selected="selected"';
        }
        ?>
         <option value="<?php echo $value['company_id'];?>"
<?php echo $selected;?>><?php echo $value['company_name'];?></option>
         <?php }?>

Javascript to execute ajax

<script>
//Inint_AJAX funnction is excluded as it has to do anything with requirement
function dochange(val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {
            document.getElementById('selectdepartment').innerHTML="";
           document.getElementById('selectdepartment').innerHTML=req.responseText; //retuen value
      }
 }
};
req.open("GET", "company.php?val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
</script>

company.php

$val=$_GET['val'];

  echo "<select name='departmentname' id='departmentname'>\n";
  echo "<option value='-1'><b>Select Department</b></option>\n";
  foreach($user_obj->getUserDepartmentname($val) as $k=>$vals){
  echo "<option value='".$vals['department_id']."'>".$vals['department_name']."</option>";
   }                  
echo "</select>\n<br>";
echo " <span class='asterisk>NOTE:Please (Re)Select the User Group everytime you change department.</span>";

User Group Select Box

<select name="usergroupname" 
onchange="javascript:dochangereport(this.value,document.getElementById('departmentname').value);
    dochangeoverride(this.value,document.getElementById('departmentname').value);" id="usergroupname">
    <option value="-1">Select User Group</option>
     <?php 
    foreach($user_obj->getUserGroupName() as $gkey=>$gval){
     $gselect = '';
     if($gval['usergroup_id'] == $user_obj->user_group_id){
     $gselect = 'selected="selected"';
     }
    ?>
    <option value="<?php echo $gval['usergroup_id'];?>"
     <?php echo $gselect;?>><?php echo $gval['usergroup_name'];?>
    </option>
     <?php }?>
     </select>

function calling ajax call

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

**reportto.php**

    $val=$_GET['val'];
    $dep=$_GET['dep'];
     echo "<select name='reportingto[]' multiple='multiple' size='3'>\n";
      echo "<option value='-1'><b>Select Supervisor(s)</b></option>\n";
      foreach($user_obj->getUserReportingto($val,$dep) as $rkey=>$rval){
echo "<option value='".$rval['user_id']."'>".$rval['user_fname']." ".$rval['user_fname']." </option>";
}echo "</select>\n";

could someone help me out in this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:18:41+00:00Added an answer on May 18, 2026 at 10:18 pm

    I’m going to guess here: you are probably using something like

    $('select.department').change(function() { (...ajax...)});
    

    Instead, use:

    $('select.department').live('change', function() { (...ajax...)});
    

    This will also match any elements that are added later and match your jQuery selector. See also: http://api.jquery.com/live/.

    EDIT:

    Ok, so I guessed wrong. If you’re not using jQuery, you will have to use event bubbling (http://www.quirksmode.org/js/events_order.html) (EDIT: apparently, onchange doesn’t bubble in IE, see Does the onchange event propagate?) or add the event handler to the newly created <select> each time you update it, using

      if (req.status==200) {
          document.getElementById('selectreport').innerHTML="";
          document.getElementById('selectreport').innerHTML=req.responseText; //retuen value
          document.getElementById('reportingto').onchange = function() {
              var val = this.value,
                  department = document.getElementById('departmentname').value;
    
              dochangereport(val, department);
              dochangeoverride(val, department);
          }
      }
    

    And then add an id to your ajaxed selectbox, like this:

    (reportto.php)
    echo "<select id='reportingto' name='reportingto[]' multiple='multiple' size='3'>\n";
    

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi All I have a project working in PHP which is divided in an
i have a dynamic php web page localhost/ctd/index.php All working good except if i
I am working on a php that application that uses ajax. On all the
I am working on a Symfony project and I currently have this: <?php echo
I'm working on a PHP project where all files are given the extension '.shtml'.
I am currently working on a php project with the user of ajax. I
I am Working in PHP MySql project, I Have A Page Called Live Information,
I am working on a project using MySQL and PHP. I will have many
I'm working on a little project, basically I have some text on my PHP/HTML
I am working on php project. All I want to do that, when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.