How to add checkbox value into database. Earlier without filtering input i used
if(!empty($_POST['submit'])){
$widgetEnabled = isset($_POST['widgetEnabled']) ? 'yes' : 'no';
$widgetWidth = $_POST['widgetWidth'];
$widgetHeight = $_POST['widgetHeight'];
}
But how do do it now?
<?php
$errors = array();
$success = NULL;
$error = NULL;
/* Form is not submitted display mysql data */
$input = array(
'widgetEnabled' => $configs['widgetEnabled'],
'widgetWidth' => $configs['widgetWidth'],
'widgetHeight' => $configs['widgetHeight']
);
/* /Form is not submitted display mysql data */
//Form is submitted, run code //
if(filter_has_var(INPUT_POST, 'submit')){
$defs = array(
'widgetEnabled' => FILTER_SANITIZE_STRING,
'widgetWidth' => array('filter' => FILTER_SANITIZE_NUMBER_FLOAT,
'flags' => FILTER_FLAG_ALLOW_THOUSAND),
'widgetHeight' => array('filter' => FILTER_SANITIZE_NUMBER_FLOAT,
'flags' => FILTER_FLAG_ALLOW_THOUSAND)
);
$input = filter_input_array(INPUT_POST, $defs);
require_once 'includes/antiCsrf/index.php';
$csrf = new antiCsrf();
if($csrf->verifyCsrfToken() === FALSE){
$errors[] = $lang['error']['a_0004'];
}
if(empty($input['widgetWidth'])){
$errors[] = $lang['error']['a_0005'];
}
if(empty($input['widgetHeight'])){
$errors[] = $lang['error']['a_0006'];
}
}
//Form is submitted and no errors detected, update db //
if(filter_has_var(INPUT_POST, 'submit') and empty($errors)){
foreach($input as $key => $value){
$query = 'UPDATE configs SET value = :value WHERE config = :key';
$update = $db->prepare($query);
$update->bindParam(':value', $value, PDO::PARAM_STR);
$update->bindParam(':key', $key, PDO::PARAM_STR);
$message = $update->execute();
}
if($message)
$success = $lang['success']['a_0002'];
else
$error = $lang['error']['a_0003'];
}
require_once 'includes/antiCsrf/index.php';
$csrf = new antiCsrf();
$smarty->assign('success', $success);
$smarty->assign('error', $error);
$smarty->assign('errors', $errors);
$smarty->assign('csrfKey', $csrf->csrfKey());
$smarty->assign('csrfToken', $csrf->csrfToken());
$smarty->assign('input', $input);
$smarty->display('widgetConfigs.tpl');
$db = NULL;
$csrf = NULL;
?>
---------------------------------------------------TEMPLATE -----------------------------------------------------------
<form method="post" class="form-horizontal">
<input type="hidden" name="csrfKey" value="{$csrfKey}">
<input type="hidden" name="csrfToken" value="{$csrfToken}">
Enabled <input type="checkbox" name="widgetEnabled" value="{$input.widgetEnabled}"{setChecked($input.widgetEnabled, 'yes')}>Yes
Width <input type="text" name="widgetWidth" value="{$input.widgetWidth}">px
Height <input type="text" name="widgetHeight" value="{$input.widgetHeight}">px
<form>
Aucun commentaire:
Enregistrer un commentaire