1

Can i write some code to execute while my check box is checked in my php code..

my declaration of check box is...

<input id="checkbox" name="click" type="checkbox" onclick="check(this)"/>

i thought to perform a function called check() while clicking the check box..

 <script type="text/javascript">
  function check(cb)
  {
      if($("input[type=checkbox]:checked"")
      {
        //my functionality and operations
      }
  }

But its not working, how can i perform the onclick event in the Checkbox's action..

5
  • 3
    Onclick happens in the browser, PHP runs on the server. You need to use AJAX if you want the onclick function to interact with the server. Commented Feb 16, 2013 at 6:47
  • You're passing the reference in the html itself, so why not just use it? Why not access cb in your JavaScript? Commented Feb 16, 2013 at 6:47
  • What's the PHP function you mentioned? Commented Feb 16, 2013 at 6:48
  • I think he/she wants to execute some php code when the checkbox is checked. Is this correct @GowthAMI? Commented Feb 16, 2013 at 6:59
  • yes, a particular code need to execute.. Commented Feb 16, 2013 at 7:13

5 Answers 5

2

First of all, there's a mistake. It should be .is(":checked").

function check(cb)
{
    if($(cb).is(":checked"))
    {
        //my functionality and operations
    }
}

And the HTML should be:

<input type="checkbox" onclick="check(this);" />

Or, if you wanna invoke a PHP Function after clicking on Checkbox, you need to write an AJAX code. If this is the case, in your if condition, and checked condition, you can call a PHP file, that calls only this function.

function check(cb)
{
    if($(cb).is(":checked"))
    {
        $.getScript("clickCheckbox.php");
    }
}

And you can write JavaScript plus PHP in the clickCheckbox.php file, say something like this:

clickCheckbox.php

<?php
    header("Content-type: text/javascript");
    unlink("delete.png");
    echo 'alert("Deleted!");';
?>

Once you click on the checkbox, and if the state is checked, it gives out an AJAX call to this PHP file, where you are deleting a file delete.png and in the echo statement, you are outputting a JavaScript alert, so that you will get an alert message saying Deleted!.

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

2 Comments

You don't use cb! you can just write if (cb.checked) or if you really want to use jquery if ($(cb).is(":checked"))
@Sanchit My mistake. Didn't notice. Updated. :)
1
$('#myform :checkbox').click(function() {
    var $this = $(this);
    // $this will contain a reference to the checkbox   
    if ($this.is(':checked')) {
        // the checkbox was checked 
    } else {
        // the checkbox was unchecked
    }
});

Where your form has id myform

1 Comment

-1 So if you click ANY checkbox in the form with id myForm this code will run. That really doesn't look like what the original user was trying to do. Moreover the question is unclear because the poster mentions the word php.
0

use

if ($('#checkbox').is(':checked'))

or inside an event

$('#checkbox').click(function(){

 if ($(this).is(':checked')){
    //your routine here if checked
 }else{
    //routine here if not checked
 }
});

Comments

0

You can put like this: Include the column checked in your table with default value NO. Then after your SELECT statement show the array.

page1.php

<input type=checkbox value="<?php $row['checked']?>" onclick="location.href = 'update.php?id=<?php echo $row['id']; ?>&checked=<?php if ($row['checked'] == 'YES') {  ?>NO<?php } else {?>YES<?php } ?>';" <?php if ($row['checked'] == 'YES') {  ?> checked <?php } ?>>

update.php

<?php  include('server.php'); ?>
<?php


$id = $_GET['id'];
$checked = $_GET['checked'];
if(isset($_GET['id']))
{
    
    
    $sql = "UPDATE table SET
    
                checked     = '$checked' 
                WHERE `id` = '$id' ";



    if ($conn->query($sql) === TRUE) 
    {
    } 
    else 
    {
        echo "Error updating record: " . $conn->error;
    }

    header('location: page1.php');
}

?>

Comments

-1

Try this one

<input id="checkbox" name="click" type="checkbox" onclick="check()"/>

//in js
if( $('input[name=checkbox]').is(':checked') ){
   // your code
}

3 Comments

Shouldn't it be [name=click]?
-1 So if you click ANY checkbox with the name checkbox, anywhere on the page this code will run. That really doesn't look like what the original user was trying to do. Moreover the question is unclear because the poster mentions the word php.
then you use with id like if( $('#checkbox').is(':checked') ){ // your code }

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.