moheb hawari
moheb hawari

Reputation: 321

How to get value from Multi CheckBox with the same name

I have a multi checkbox with the same attr name like "Check" but every checkbox has different value

So I have to create a jQuery code that if I press on the checkbox will do some thing in php like this code

$(document).ready(function(){
   var NewsPaperId;
   var UserId;
    var CategoryId;
    $("input[type='checkbox']").click(function() {

        if ($(this).is(':checked')) {
            NewsPaperId= $('input[name="Check"]:checked').val();
            UserId= $(".userId").val();
            CategoryId= $(".CategoryId").val();
            alert(NewsPaperId);
            $.post("AddAndDelete.php",
                {
                    Add:1,
                    UserId: UserId,
                    NewsPaperId: NewsPaperId,
                    CategoryId:CategoryId
                },
                function(data, status){
                    alert("Data: " + data + "\nStatus: " + status);
                 });
         } else {
            NewsPaperId= $('input[name="Check"]:checked').val();
            UserId= $(".userId").val();
            CategoryId= $(".CategoryId").val();

            alert(NewsPaperId);

            $.post("AddAndDelete.php",
                {
                    Delete:1,
                    UserId: UserId,
                    NewsPaperId: NewsPaperId,
                    CategoryId:CategoryId
                },
                function(data, status){
                    alert("Data: " + data + "\nStatus: " + status);
                });
        }
    });
}); 

and here is the checkbox code created by php

<?php

    $i=1;
    $sql = "SELECT NewsPaperStatus.*,UserChoises.*"
         . " FROM NewsPaperStatus"
         . " LEFT JOIN UserChoises"
         . " ON NewsPaperStatus.Id=UserChoises.NewsPaperId"
         . " WHERE NewsPaperStatus.CategoryId=$Categoryid";

    $query = mysql_query($sql);
    while ($row = mysql_fetch_assoc($query)){
      if($i==1){

          if($row['Id']==$row['NewsPaperId']){
             echo '<tr><th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="Check" value="'.$row['Id'].'"/></th>';
           }else{
               echo '<tr><th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" name="Check" value="'.$row['Id'].'"/></th>';
             }
         }
         else if($i==2){
             if($row['NewsPaperId']==$row['Id']){
                 echo '<th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="Check" value="'.$row['Id'].'"/></th>';
             }else{
                echo '<th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" name="Check" value="'.$row['Id'].'"/></th>';
              }
           }
           else if($i==3){
             if($row['NewsPaperId']==$row['Id']){
                echo '<th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" checked="checked" name="Check" value="'.$row['Id'].'"/></th></tr>';
             }else{
               echo '<th><img src="../images/NewsPaper/'.$row['Logo'].'"/></th><th><a href="">'.$row['Name'].'</a></th><th><input class="check" type="checkbox" name="Check" value="'.$row['Id'].'"/></th></tr>';
            }

       $i=0;
       }
     $i++;
    }
  ?>

so the problem is when I press on any check box its work good but when I unchecked another on it take the last vale of check box (( if I press on checkbox value 16 and press on checkbox value 17 so its work good but when I want to uncheck checkbox of value 16 the value it be 17 its take the last value of checkbox I was checked.

Upvotes: 7

Views: 4277

Answers (1)

Code Lღver
Code Lღver

Reputation: 15593

In the below line:

if ($(this).is(':checked')) {
   NewsPaperId= $('input[name="Check"]:checked').val();

You are taking the value of checked checkbox and you have already put the condition of checked in if condition. so my suggestion is to replace the

NewsPaperId= $('input[name="Check"]:checked').val();

above line with following line:

NewsPaperId= $(this).val();

And in the else part you need to check if there is any checked checkbox or not. here is code for it:

else {
    $('input[name="Check"]').each(function(){
        if($(this).is(':checked')){
           NewsPaperId= $(this).val();
        }
    });
    if(NewsPaperId != ''){ //do your stuff}

Here is the reason to make the if condition. The reason is if user click on checked checkbox and other checkbox is not checked then the value of NewsPaperId will be blank else if will take the value of other checkbox which will be checked.

Upvotes: 2

Related Questions