MGR
MGR

Reputation: 384

checkbox onclick in php function

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..

Upvotes: 1

Views: 34632

Answers (5)

user13054316
user13054316

Reputation:

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');
}

?>

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

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!.

Upvotes: 2

Keith A
Keith A

Reputation: 811

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
 }
});

Upvotes: 0

Nilesh Gupta
Nilesh Gupta

Reputation: 367

Try this one

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

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

Upvotes: -1

Deadlock
Deadlock

Reputation: 1577

$('#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

Upvotes: 1

Related Questions