0

Here's my problem: I have an html template with a checkbox. When the php script runs, it passes a value to the checkbox's "checked" property like this:

<input type="checkbox" name="form_data[show_products]" {SHOW_PRODUCTS} />

where {SHOW_PRODUCTS} can be either 'checked="true"' or '', depending on whether the data in the database column is 1 or 0. So, in other words, the checkbox is checked, or unchecked, in accordance with the data passed from the database.

Now, since this is the template for the edit form, I need to gather the value of the "checked" property and pass it to the database. However, if I uncheck the checkbox that has been checked automatically at page load and try to save changes, the script still writes 1 to the database instead of 0. How can I change this?

EDIT

These are the php parts that handle the data:

else if (isset($_POST['edit']))
{   
    $form_data = $_POST['form_data'];
    $present_id = $form_data['present_id']; 
    $present=new Present();
    $present->load($present_id); 

    if (isValidForm())
        {       
        $present->set($form_data);
        $present->update();
        }
    else
        {
        $errors=$formValidator->getErrors();
        $page_content_HTML=edit_form_HTML($form_data, $errors);
        }
}

function set($data){
    $keys=array_keys($data);
    foreach($keys as $key)
    {
        $this->$key=$data[$key];
    }   
}

function update(){
        global $db;
        $date_from = format_datetime($this->date_from, "%Y.%m.%d. %H:%M:%S");
        $date_to = format_datetime($this->date_to, "%Y.%m.%d. %H:%M:%S");
        $sql="UPDATE presents SET present_name='".$this->present_name."', description='".$this->description."', present_image='".$this->present_image."', date_from='".$date_from."', date_to='".$date_to."', short_description='".$this->short_description."', show_products='".$this->show_products."' WHERE present_id='$this->present_id'";
        $db->query($sql);

}
5
  • Can you post the script you use to save the checkbox statuses? Commented Jul 30, 2012 at 11:12
  • How is the generated source code in your browser of this checkbox? Commented Jul 30, 2012 at 11:12
  • Post your HTML/php script that handles this.. Commented Jul 30, 2012 at 11:14
  • I added the php parts that handle the data Commented Jul 30, 2012 at 11:25
  • @MarkoCakic have you tried isset Commented Jul 30, 2012 at 11:37

5 Answers 5

1

The problem is not the form / checkbox but how you handle the request data in your PHP script. As I commented before: An unchecked checkbox is not part of the POST or GET request (it's not just empty but it's not existent).

Therefore, $this->show_products does not exist when the box is unchecked.

Try following: Add the line

$present->show_products = 0;

right before

$present->set($form_data);

This makes show_products default to "off" ("0") and it only gets overwritten if the checkbox is checked.

(Btw: I also think, if the CB is checked that the value will be "on" an not "1". Add the attribute value="1" to the checkbox tag to make it "1" instead).

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

2 Comments

I tried it, I put the line you wrote before the set() and tried saving with the box checked but it writes 0 to the database. It is logical to me too, that it should overwrite the show_product attribute in the set() function if the checkbox is checked, but somehow it doesn't...
Yeah, I forgot the "value" line! Now it works like a charm! Thanks a lot
1

According to the W3C the checked value is a boolean value. So if its unchecked set it to false instead of ''.

checked [CI] (CI meaning Case Insensitive)

When the type attribute has the value "radio" or "checkbox", this boolean attribute specifies that the button is on. User agents must ignore this attribute for other control types.

3 Comments

Nope, I tried that first, but, when I set it to checked="false" it still checks the checkbox. I read somewhere that if the "checked" property is set in any way, it will check the box - the only way to leave it unchecked is to pass nothing.
Maybe remote the entire checked attribute then instead of just removing the value. The "checked" is optional.
@MarkoCakic: I can confirm what you read is right. I'd look into a var_dump before saving to the database and after fetching in various places to spot the problem. Sometimes the data isn't conveyed correctly, it's a good practice to dump your arrays once in a while when programming to see if you're working with the expected set of data.
1

Another way of checking checkboxes is to use isset

if(isset($_POST['checkboxname'])){
$status=1;
}else {
$status=0;
}

1 Comment

Sorry for not commenting. Your way would probably given me the solution. I chose another answer since it solved my problem more quickly but I voted up your answer. Thanks again
1

It's a model-view-controller paragdim? I guess your problem is that you are updating only the fields that are posted in your query. Hence the checkbox isn't checked, it isn't sent, so isn't updated neither.

Try setting something like

$_POST['theCheckbox'] = isset($_POST['theCheckbox']);

If it is set, leave it to true, if it isn't set, put false. So now your input always have a value of true or false, so it'll get updated by your model.

4 Comments

That's probably -close- but not the solution. Tho it should lead you to your problem and what to look into.
Hm, I think that might be the problem. I will look into it now, it will probably work...
You may do a check elsewhere like !empty or 'true' that could conflict. I mean I don't have much material to really help, but this very problem got me a few times at work.
I chose another answer because it got my problem solved quickly, but I believe your way would've done it as well. Thanks anyway!
0

Try to use checked="checked" or no property at all for unchecked check-box.

2 Comments

But if the checkbox is automatically checked at page load, then it's "checked" property has already been set, and whatever I do as a user (uncheck manually), it still passes the value as if it is checked...
It actually doesn't, an unchecked checkbox is not part of the POST or GET request (it's not just empty but it's not existent). I think you rather have a problem in yout php script handling the request. It would be helpful to see, what you do with the request in PHP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.