0

i need to know how on earth to get my checkbox value from PHP that is in a loop and also might be in DOM. I have to put that checkbox inside the loop to make it being shown on each row of databases. I tried to call it back using different method but none success. The last part is javascript but i don't have any clue how to do that.

My code for javascript index.php.

 function ajaxSearchUpdater(p){
        $("#result").show();
        var x = $("#search").val();
        var y = $("#edulevel").val();
        var pagelim = $("#pagefpe").val();
        var pagenumber = p;
        var checkb = $(".sBorrow").val()
        $.ajax({
            type:'POST',
            url:'userres.php',
            data:'q='+x+'&e='+y+'&pagelim='+pagelim+'&pageno='+pagenumber+'&checkb='+checkb,
            cache:false,
            success:function(data){
                $("#result").html(data)
            }
        });
    }



    $(document).ready(function(e) {
        ajaxSearchUpdater(1);               // fires on document.ready
        $("#search").keyup(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $("#edulevel").click(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $("#pagefpe").click(function() {
            ajaxSearchUpdater(1);           // your function call
        });
        $('.sBorrow').on('change', function(){
            var checkBorrow = $(event.target);
            var isChecked = $(checkBorrow).is(':checked');
            alert("test");
            alert(isChecked);
            alert('checkbox'+checkborrow.attr('id')+'is checked:'+isChecked);
        });
    });

    $(document).ready(function() {
      $('.sBorrow').on('change', function(event) {
        var checkbox = $(event.target);
        var isChecked = $(checkbox).is(':checked');
        alert('checkbox ' + checkbox.attr('id') + ' is checked: ' + isChecked);
      });
});

My code for the checkbox in PHP userres.php

if($stmt->rowCount() > 0){
    $r=$stmt->fetchAll();
    echo "<table class='tablesorter-blackice' id='myTable' style='width:97%; table-border: 1'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>No.</th>";
        echo "<th>No.Matric</th>";
        echo "<th>Name</th>";
        echo "<th>Programme</th>";
        echo "<th>Title</th>";
        echo "<th>Thesis Level</th>";
        echo "<th>Serial Number</th>";
        echo "<th>Availability</th>";
        echo "<th>Select book (Max 3)</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

    foreach($r as $row){
            $sBorrow = $_SESSION['sBorrow'];

            echo "<tr align='center'><td>". ($startrow+1) ."</td><td>". $row['matricno'] ."</td><td>". $row['studentname'] ."</td><td>". $row['programme'] ."</td><td>". $row['title'] ."</td><td>". $row['thesis_level'] ."</td><td>". $row['serialno'] ."</td><td>". $row['bavailable'] ."</td><td>
            <form method='post'>
            <input type='checkbox' name='sBorrow' id='sBorrow' class='sBorrow' value='". $row['serialno'] ."'>
            </form></td></tr>";
            $startrow++;
            //echo $row['education_level'];
    }
    echo "</tbody>";
    echo "</table>";

I don't know what to do since i'm calling that page from ajax and uhh how should i explain this. You know index.php -> userres.php -> index.php using ajax. for javascript on the bottom part is what i have done and i dont think its right. I tried to create one other document ready for this checkbox but still even alert not showing up. I'm confused. please help T_T

AhKing
  • 171
  • 2
  • 14
  • change `$('.sBorrow').on('change', function(event) {` into `$(document).on('change','.sBorrow', function(event) {` delegate the event properly – guradio Jul 28 '16 at 03:17
  • @guradio no it wont work. i just follow the tutorial here, http://stackoverflow.com/questions/14858206/get-checkbox-value-without-page-refresh, but its not working for me but working for him. Maybe because he is creating simple html and then use javascript. – AhKing Jul 28 '16 at 03:28
  • i also try to tutorial from here, http://stackoverflow.com/questions/33393852/is-it-possible-to-submit-a-checkbox-form-without-submit-button-in-php, but the problem is i can't set if else statement inside the echo. I was thinking to session the value but well if else statement is kinda problem inside the form – AhKing Jul 28 '16 at 03:33
  • you are adding the class dynamically you should delegate the event did you try it? – guradio Jul 28 '16 at 03:36
  • oh my bad. Apologize for asking but how should i start? it is something like this ? `$( "table" ).delegate( "td", "click", function() { $( this ).toggleClass( "chosen" ); });` – AhKing Jul 28 '16 at 03:41
  • what version of jquery do you use?if you are using newer version use `.on()` like the way i commented before – guradio Jul 28 '16 at 03:45
  • im using new one.3.1.0. Well i never use delegate before. Based on coding i commented earlier, is that what you mean by delegate? – AhKing Jul 28 '16 at 03:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118492/discussion-between-guradio-and-ahking). – guradio Jul 28 '16 at 03:49

0 Answers0