0

I've run into an issue I find very confusing. I have a form with one checkbox. I need the form to apply a value of 1 or 0 if the checkbox is checked or not.

<input type="checkbox" name="admin">

Then the PHP, something like this:

$update_data['admin'] = isset($_POST['admin']) ? '1' : '0';

It works if it's checked. I'm not sure how to go about getting the "off" state.

2
  • So is it working or not? Commented Feb 20, 2013 at 5:51
  • If the checkbox is checked, it works. When it isn't checked, I'm not getting a value of 0. Commented Feb 20, 2013 at 5:52

6 Answers 6

1

PHP Code need to be changed

$update_data['admin'] = empty($_POST['admin']) ? '0' : '1';

isset() will only check whether the value is set i.e. Null or Not. In ur case it set to ZERO

empty on the other hand check for both value is set to null and value is Zero if either of them true then return true .

and in HTML

<input type='checkbox' value ='1' name='admim'>
Sign up to request clarification or add additional context in comments.

Comments

1

When checkbox is submitted with no value, its value is 'on' if retrieved from $_POST['admin']. I guess you can also do

$update_data['admin'] = ($_POST['admin'] === 'on') ? '1' : '0';

5 Comments

But how would I get the "off" ?
Why do you need to get "off"? If it's not "on", update_data['admin'] becomes '0'. Isn't that what you want to achieve?
To add, it would be better idea to check isset($_POST['admin']) before checking if it has value 'on'. Otherwise, it will throw a notice: undefined index, when it's not set. I prefer working with no notice at all.
It's "on" whether checked or not. It always returns a value of 1.
Did you fix the problem? I am not sure why it is getting 'on' all the time.
0

Checking if something is defined with isset is different from getting its value. Try this:

 <input type="checkbox" name="admin" value="checked_or_whatever_value_you_like">
 if(isset($_POST['admin'])){
     $update_data['admin'] = $_POST['admin'] == 'checked_or_whatever_value_you_like' ? '1' : '0';
 }

Comments

0

Everything should work as intended :)

Just to make sure that for was submitted:

admin

If checkbox is unchecked, the value (and it's key) wont be passed to query string.

So, check

If (isset($_POST['submit'])){
  $update_data['admin'] = isset($_POST['admin']) ;
  //actions if form has been submitted.
}

Comments

0

It seems to work with me, here is my sample code :

<form action="" method="POST">
    <input type="checkbox" name="admin">
    <input type="submit">
</form>
<?php 
    $check = isset($_POST['admin']) ? '1' : '0';
    echo $check;
    var_dump($check);
?>

it will produce 1 string(1) "1" when you check the checkbox and 0 string(1) "0" if it's unchecked.

Comments

0

I found the answer in another post. With:

$update_data['admin'] = ($_POST['admin'] == 1) ? '1' : '0';

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.