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