Marko Cakic
Marko Cakic

Reputation: 7106

HTML checkbox checked

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

}

Upvotes: 0

Views: 946

Answers (5)

u_mulder
u_mulder

Reputation: 54796

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

Upvotes: 0

MiDo
MiDo

Reputation: 1057

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

Upvotes: 1

user1454661
user1454661

Reputation:

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.

Upvotes: 1

Sibu
Sibu

Reputation: 4617

Another way of checking checkboxes is to use isset

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

Upvotes: 1

nyxthulhu
nyxthulhu

Reputation: 9762

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.

Upvotes: 1

Related Questions