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.
If a set of checkboxes have the same name the HTML form encoding is different to those with different names.
change(function() { $("#myform input:checkbox"). attr("checked", false); $(this). attr("checked", true); }); This should work for any number of checkboxes in the form.
Each check box must have a unique name. Radio buttons allow only one choice within a group of buttons. Each radio button within a group should have the same name. You can create more than one group of radio buttons by using different names.
Simply check or uncheck multiple checkboxes at a time by clicking and dragging. Allows you to check multiple checkboxes quickly by CLICKING & DRAGGING or even quicker with an ALT+CLICK & DRAG area select.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With