0

I have this checkbox:

<label>Maintenance Mode: <small>Tick to enable/disable.</small></label>
<input type="checkbox" name="maintenance" value="1" <?php echo $maintenance; ?> >

I get the data from my database. So, if maintenance is enabled in my database, then the checkbox will be checked on. That happens like this:

if($sdata['maintenance']==1):
    $maintenance="checked='checked'";
else: 
    $maintenance="";
endif;

The problem is, whenever I want to disable maintenance, it doesn't update the database. It will still be set to "1".

This is how I post it:

$maintenance = inputFilter($_POST['maintenance']);

My database table have two columns:

setting_name and setting_value. In this case, it would be:

maintenance and 1

I update it like this:

foreach($_POST as $key => $value)
{
  $value = inputFilter($value);
  mysql_query("UPDATE settings SET setting_value='$value' where setting_name='$key' limit 1")
  or die(mysql_error());
}

Whenever I uncheck the checkbox, and save the settings, nothing happens. I even tried to echp $maintenance from the post, and when it's unchecked, the value is 0.

Anyone have any idea what is causing this?

1
  • Webbrowser doesn't send checkbox in the POST request then is non-checked. Commented Jan 3, 2014 at 11:52

4 Answers 4

5

Non-checked checkboxes are not send in the POST request, so its key is not in $_POST

You should check if the checkbox name exists.

if(!isset($_POST['maintenance'])) {
     // Checkbox is unchecked
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried to do this: if(!isset($_POST['maintenance'])) { $maintenance = 0; } .. still doesnt work.
Look at your loop, you are looping through $_POST and $_POST does not contain 'maintenance' key
I am not following. Sorry. Would you mind updating your answer, with your example?
Please try to understand what $_POST is and that you are doing a foreach on that variable and you don't have the 'maintenance' key there so it will never be modified on the database.
But I have other fields in my form, such as normal text inputs, and that works.
|
0

If an checkbox is unchecked it is not set in your $_POST. You have to check something like this:

if(isset($_POST['maintenance']) && $_POST['maintanance'] != 0) {
    //is checked
} else {
    //is unchecked
} 

2 Comments

@Alex-barroso beat me to it ;)
I tried this. If it was checked, I put hte $maintenance = 1 and if unchecked, $maintenance = 0. Didn't work..
0

You can try this:

<?php

if($_SERVER['REQUEST_METHOD'] == 'GET') {
    if($sdata['maintenance']==1):
        $maintenance="checked='checked'";
    else:
        $maintenance="";
    endif;
}
elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(array_key_exists('maintenance',$_POST)) {
        $maintenance="checked='checked'";
    }
    else{
        $maintenance="";
    }
}

Comments

0

Place the default (off) value in hidden input before checkbox:

<input type="hidden" name="maintenance" value="0"/>
<input type="checkbox" name="maintenance" value="1" <?=($sdata['maintenance']==1 ? 'checked' : '')?> >

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.