0

I have a problem with my code. The checkbox check/uncheck all works on first load as all employee list are displayed. But when I select the dropdown for a specific group of employees, it displays the selected group of employees but the check box check/uncheck all doesn't work anymore. I am using xmlhttprequest on searching a group of employees. Please help. I have been a stackoverflow searcher on all my codes but even searching all over the internet I couldn't find the right answer.

Here's a sample of my code on dropdown onchange:

function searchStatus(str) {
document.getElementById("livesearch").innerHTML="";
$("#dvloader").show();
if (str.length==0) {
document.getElementById("livesearch").innerHTML="";
//document.getElementById("livesearch").style.border="0px";
$("#dvloader").hide();
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 && xmlhttp.status==200) {
  document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
  $("#dvloader").hide();
  //document.getElementById("livesearch").style.border="1px solid #A5ACB2";

    /// can add another function here
}
}

xmlhttp.open("GET","findStatus.php?employee="+str,true);
xmlhttp.send();

}

A sample code or program would be very helpful. Thank you very much in advance.

Here's what the div "livesearch" looks like to give you an overview of what's really happening:

<div id="livesearch" class="span12">

<?php
// SQL query
//$strSQL = "SELECT * FROM employees WHERE '$status' = '$status' ORDER BY emp_lname 
ASC";

//$employee = ucwords($employee);

            $strSQL =  "SELECT * ";
            $strSQL .= "FROM employees ";
            $strSQL .= "ORDER BY emp_lname ASC ";

            //In MySQL syntax ucase function is the same with the PHP's
            //ucase function

// Execute the query (the recordset $rs contains the result)
            $rs = mysql_query($strSQL);
            $num = 0;

            $rec_count = mysql_num_rows($rs);
            ?>
            <?php
            if($rec_count < 1){
            ?>
            <div class="alert alert-info">
        <button class="close" data-dismiss="alert" type="button"></button>
            <strong>Information: </strong>No results. Search again.
            </div>
            <?php
            } else {

            }

            ?>

<form class="" action="" method="post" enctype="multipart/form-data" name="selection">
<table align="center" class="table table-hover">
<thead>
<tr>
<th><input class="checkall" type="checkbox" value="" onclick="check();"/></th>
<th>Employee Number</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php


            // Loop the recordset $rs
            while($row = mysql_fetch_array($rs)) {
            $num++;
            ?>
<tr>
<td><input class="checkbox1" name="checkbox[]" id="checkbox[]" type="checkbox" 
value="<?php echo $row['emp_num'];?>"/></td>
<td><?php echo $row['emp_num'];?></td>
                        <td><?php echo $row['emp_fname'];?></td>
                        <td><?php echo $row['emp_mname'];?></td>
                        <td><?php echo $row['emp_lname'];?></td>
<td><?php echo $row['emp_status'];?></td>
<td><a href="basic.php?emp_num=<?php echo $row["emp_num"];?>">View Records</a></td>
                        </tr>
            <?php
            }
//<td><a href='basic.php?emp_num={$row["emp_num"]}'>View Records</a></td>
//<td><a href='basic.php?delete_emp={$row["emp_num"]}'>Delete Employee</a></td>
            ?>
</tbody>
</table>

</form>

</div>
  • why are you using `XMLHttpRequest` instead of [jQuery.ajax()](http://api.jquery.com/jQuery.ajax/) – Arun P Johny Jun 17 '14 at 02:48
  • can you share the code that does check/uncheck all functionality... sounds like you are not using event delegation to dynamic elements – Arun P Johny Jun 17 '14 at 02:49
  • whether the check all is inside the `livesearch`? – Arun P Johny Jun 17 '14 at 02:53
  • @ArunPJohny Here's my check/uncheck all: `$(document).ready(function() { $('.checkall').on('click', function () { $(this).closest('table').find(':checkbox').prop('checked', this.checked); }); });` – chuybenchuy Jun 17 '14 at 02:53
  • @ArunPJohny no, it's in javascript..i named it check.js..sorry for a very noob question..newbie here.. :( – chuybenchuy Jun 17 '14 at 02:56

1 Answers1

0

The problem is you are dealing with dynamic elements and is not using event delegation to handle that case.

So use event delegation syntax of .on() to handle dynamic elements

$(document).on('click', '.checkall', function () {
    $(this).closest('table').find(':checkbox').prop('checked', this.checked);
});

Since you are already using jQuery there is no need to use XMLHttpRequest to do the ajax request and worry about the browser comparability issues... use jQuery.ajax() or in this case the .load() method like

function searchStatus(str) {
    var $live = $("#livesearch").empty();
    if (str.length == 0) {
        return;
    }
    $("#dvloader").show();
    $live.load("findStatus.php?employee=" + str, function () {
        $("#dvloader").hide();
    })
}

Event binding on dynamically created elements?

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • still not working..actually..i do my search on basic.php..when the user searches using dropdown, findstatus.php gets the value of the dropdown and displays a new set of employee list based on the dropdown value. – chuybenchuy Jun 17 '14 at 03:07
  • can you add more samples? I'm really not good at this but I know I am asking the right people here who has great talents in programming. – chuybenchuy Jun 17 '14 at 03:19
  • @user3746866 can you try `$(document).on('click', '.checkall', function () { $(this).closest('table').find(':checkbox').prop('checked', this.checked); });` – Arun P Johny Jun 17 '14 at 03:21
  • also tried your code just now..still not working..it only works on first load with all the employees list..but doesn't work after ajax refresh on user selection with selected group.. :( – chuybenchuy Jun 17 '14 at 03:26
  • @user3746866 after the ajax request is there an element with class `checkall` – Arun P Johny Jun 17 '14 at 03:31
  • yes..there is..inside the `findStatus.php` the check/uncheck all button is `class="checkall"` the check/uncheck all inside `basic.php` also has `class="checkall"` – chuybenchuy Jun 17 '14 at 03:39
  • sorry for the late reply..i was busy adding codes to my question..you can see some changes and an overview inside `
    `
    – chuybenchuy Jun 17 '14 at 03:40
  • i think i just solved my own problem..but your code really helped me think harder..and even made my code shorter..very much thanks.. :) – chuybenchuy Jun 17 '14 at 03:48
  • @user3746866 what was the problem – Arun P Johny Jun 17 '14 at 03:55
  • `$(document).on('click', '.checkall', function () { $(this).closest('table').find(':checkbox').prop('checked', this.checked); });` - this code works brilliantly but at first it wasn't working because the `class="checkall"` was not found in the `findStatus.php` on the check/uncheck all checkbox..THANK YOU VERY MUCH..you really greatly helped me on this.. :) – chuybenchuy Jun 17 '14 at 04:11