0

I have an HTML which displays data from a database table. There'a checkbox field, which stores id of the user. And when I click on a row, that ID has to be passed to another page using a JavaScript function. But I have encountered a few errors. I don't know where I went wrong.

This is my Javascript function. It is used to make the table rows clickable. SO when I click on a row, the corresponding ID is passed to editgroup.php page.

<script>
    $('table.table tbody tr').click(function(event)
    {
       if(event.target.type === 'checkbox')
       {
            event.preventdefault()
       }
       else
       {
        window.location='edituser.php?val=<?php echo $id; ?>';
       }
    }); 
</script>

Below given is my table:

<table id="edituser" class="clickable"
    <thead>
        <th></th>
        <th>UserID</th>
        <th>Username</th>
    </thead>
    <tbody>
        <?php
            for($i=0; $i<$x; $i++)
            {
             $id= $val_array[$i]['id'];                                  
            $userName= $val_array[$i]['userName'];
        ?>
        <tr>
            <td class="text-center"><input type="checkbox" value="<?php echo $id;  ?>"></td>
            <td><?php echo $id;  ?></td>
            <td><?php echo $userName;  ?></td>
        </tr>
        <?php } ?>
    </tbody>
</table>
3
  • You can't write javascript mixed up with php Commented May 22, 2014 at 9:58
  • Is there a solution??? @Ranjith Commented May 22, 2014 at 10:01
  • @TheJoker I posted my answer. Commented May 22, 2014 at 10:12

3 Answers 3

2

you have to make some change

First in your tr add id like this

<?php
for($i=0; $i<$x; $i++)
{
 $id= $val_array[$i]['id'];  //data from database                                
$userName= $val_array[$i]['userName']; //data from database  
?>
<tr id="<?php echo $id;?>"> <!-- add id here -->
<td ><input type="checkbox" value="<?php echo $id;  ?>"></td>
<td><?php echo $id;  ?></td>
<td><?php echo $userName;  ?></td>
</tr>
<?php } ?>

now your JS

<script>
            $('table.clickable tbody tr').click(function(event){
                if(event.target.type === 'checkbox'){
                    event.preventdefault()
                }else{
                    var val = $(this).attr('id');
                    window.location='edituser.php?val='+val;
                }
            }); </script>
Sign up to request clarification or add additional context in comments.

4 Comments

returns undefined. @Satish Sharma
@TheJoker wrong class is applied in the jquery i had changed it see the updated jquery function and try now. i had added class clickable in jquery
returns undefined again. @Satish Sharma
make some more changes in jquery function try now
0

Your Javascript will be

      $('table.table tbody tr').click(function(event){
            if(event.target.type === 'checkbox'){
                event.preventdefault()
            }else{
                var value = (this).find("input[type='checkbox']").val();   /* Get value from clicked tr Checkbox */
                window.location.replace('edituser.php?val='+value); /* Pass the value */
            }
        });

Comments

0

Create new function with dynamic id for your table row

<tr onclick="edit_user(<?php echo $id;?>)" >
      ....
</tr>

Then you can find your row of where you have to click

<script>
    function edit_user(user_id) {
       if(user_id != '') {
           window.location='edituser.php?val='+user_id;
       }
    }
</script>

Or else

This is more even simple your code.

<input type="checkbox" value="<?php echo $id;  ?>" onclick="document.location.href='edituser.php?val=<?php echo $id; ?>'">

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.