mercredi 29 avril 2015

How to get value from Multi CheckBox with the same name

hello guys , I hope this is not a stupid question.

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

so i have 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

please help me 4 days and i cant resolve the problem.




Aucun commentaire:

Enregistrer un commentaire