1

i'am working on checkboxes when user click checkbox i am checking is it checked or not if checked i update value on database here is my code

$(document).ready(function(){
    $('input[type="checkbox"]').bind('click',function() 
    {
         var tObj = $(this).val();
         if ($('#checkpermission').is(':checked'))
         {
            alert(tObj);
            $.ajax({
                  url: 'checkpermission.php',
                  data: {"Selected":tObj},
                  type: 'post',
                  success:function(data)
                  {
                     alert(data);
                     //Succes!
                  }
            });
         }
         else
         {
             // alert(tObj);
             $.ajax({
                   url: 'uncheckpermission.php',
                   data: {"Selected":tObj},
                   type: 'post',
                   success:function(data)
                   {
                      alert(data);
                      //Succes!
                   }
             });
         }
    });

});


   <?php
session_start();
require 'dbconnect.php';
$page_name="RegisterUsers.php";
$start=$_GET['start'];
if(strlen($start) > 0 and !is_numeric($start))
{
echo "Data Error";
exit;
}
$eu = ($start - 0);
$limit = 8;
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;



$query="select * from tbl_user where username!='admin' and password!='admin'";
$result=mysql_query($query);
$nume=mysql_num_rows($result);
if($nume>0)
{


if($_REQUEST['SearchText'])
{

$find=$_REQUEST['SearchText'];
$query="select * from tbl_user where username!='admin' and password!='admin' and username LIKE'%$find%' or Email LIKE'%$find%' or PhoneNumber LIKE'%$find%' order by id limit  $eu, $limit";
}
else
{
$query="select * from tbl_user where username!='admin' and password!='admin' order by username limit  $eu, $limit";
}
$result=mysql_query($query);
while($data=mysql_fetch_row($result))
{
 if($data[5])
 {
 $path=$redirect."/services/User/".$data[5];
 }
 else
 {
 $path=$redirect."/images/noimage.png";
 }
?>
 <tr>
<td class="gallery">

<a href='<?=$path ?>'>
 <img width="50" height="50" src=<?php echo $path; ?> />
 </a>

</td>
<!--<td id='myText'><img width=50 height=50 src=<?php echo $path; ?> /></a></td>-->
 <td><?=$data[1]?></td> 
 <td><?=$data[3]?></td>
 <td><?=$data[4]?></td>
 <td>
 <div style="padding-left:25px;">
 <INPUT TYPE="checkbox" class="checkpermission" name="checkuser" id="checkpermission" value='<?=$data[0]?>'<?php if($data[6]=="Read-Write"){echo "checked";}?>>
 </div>
 <div>
 Read-Write
 </div>
 </td>

<td class='delete'><a  href='http://ifliptips.com/admin/VueGuides/AddUser.php?id=<?=$data[0]?>&action=edit' >Edit</a></td>
 <td class='delete'><a href='http://ifliptips.com/admin/VueGuides/RegisterUsers.php?id=<?=$data[0]?>&action=delete' onClick="return deletepressed();">Delete</a></td>
 </tr>
<?php
}
}
?>

every time when user click goes on if condition only else loop not working i am seeing response

so can any one guide me how can i overcome this Thanks for advance.

2
  • what error you are getting with this code. Commented Feb 7, 2012 at 5:37
  • every time $('#checkpermission').bind('click', function() { alert($("#checkpermission").is(':checked'));//true every time }); Commented Feb 7, 2012 at 6:24

3 Answers 3

1

Try to use

if($("#checkpermission:checked").length > 0)

Solution

There were more than one check box with checkpermission. So the code needs to be modified like this

$('input[type="checkbox"]').bind('click',function() 
{
     var tObj = $(this).val();
     if ($(this).is(':checked'))
     {
         //Rest of the code

Ajay will change the code to generate unique ids for the checkbox. Above code will work for this case too.

Check this jsFiddle

Hope this works for you.

Sign up to request clarification or add additional context in comments.

6 Comments

What are you doing inside onchange? Can you bind change event using jQuery? Check this jsfiddle.net/G9sLY/5
every time $('#checkpermission').bind('click', function() { alert($("#checkpermission").is(':checked'));//true every time });
Where did you test it? In your project or on jsFiddle. Also let me know which browser you are using? Is it happening on browsers?
i am testing on project i am using chrome
I'm not a PHP developer but can you remove value='<?=$data[0]?>'<?php if($data[6]=="Read-Write"){echo "checked";}?> this and try? What is this code doing? It might be setting checked always.
|
1

$('input:checkbox:checked').val() should work :)

2 Comments

every time i click check box it goes if ($('#checkpermission').is(':checked')) { alert(tObj); $.ajax({ url: 'checkpermission.php', data: {"Selected":tObj}, type: 'post', success:function(data) { alert(data); //Succes! } });
@AjayChthri else condition will only work when you uncheck the checkbox
0

As you are putting "if condition" after click happen so your "else condition" will work only when you uncheck the checkbox.

check this example: i have putted one alert in else condition.

http://jsfiddle.net/yHZsG/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.