I have added a checkbox to unsubscribe to the php susbcription form. Also have added a new field to the database with the field Name user_out and Type tinyint(1). I am not sure how to pass the checkbox value to the database.
As i can see you printing checkboxgroup empty ? 'user-out', ' '
You can simply check if checkbox value is 1, then put 1 or 0 instead of $_POST['user-out']
if (isset($_POST['submit'])) {
if (isset($_POST['user-out']) && $_POST['user-out'] == '1') { // if checkbox value is 1
/*
UPDATE database, set 1 to user-out
if you want to update user-out to be 0 after unsubscribe you can remove $_POST['user-out'] and set it to 0 manually in code
*/
$insert['ID'] = Mysql::SQLValue('');
$insert['user_name'] = Mysql::SQLValue($_POST['user-name']);
$insert['user_email'] = Mysql::SQLValue($_POST['user-email']);
$insert['user_out'] = Mysql::SQLValue($_POST['user-out']); // instead of $_POST['user-out'] put '0'
if (!$db->insertRow('subscribers', $insert)) {
$user_message = '<p class="alert alert-danger">' . $db->error() . '<br>' . $db->getLastSql() . '</p>' . "\n";
} else {
$user_message = '<p class="alert alert-success">Thanks for subscribing!</p>' . "\n";
Form::clear('newsletter-suscribe-form');
}
}
}
Yes this is the right idea. I do get a warning error when running the form with the checkbox checked.
Warning: strlen() expects parameter 1 to be string, array
Not sure if the values I entered for the field user_out in the database table are correct.
Field: user_out
Type: tinyint(1)
Null: Yes
Defaul: NULL
Change to if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') to check if checkbox value is 1
Next add on top of script error_reporting(E_ALL); and var_dump($_POST); try to see what you get with $_POST when you submit your form, it will point you to right direction and throw you actual error.
Actually, I am not getting an error any longer. The opt_out field in the database only records NULL regadles if the checkbox is checked instead of 1 or 0;
If this is Unsubscribe form, you donât need ELSE in your statement because you only checking if checkbox is checked, and updating column opt_out with 1.
The if statement is not producing errors anymore. The form is meant as a signup feature. The checkbox is and additional option in the form for subscribers that might want to unsubscribe. The value of the opt_out filed in the database s alway NULL regardless if the checkbox is checked or unchecked. Instead it should be 1 or 0.
Change in database those options Name : opt_out, Type : tinyint(1), Null: No, Default : 0 this will set default value to 0, and for unsubscribe will be set to 1.
And change
to
// if checkbox is checked, opt_out will be set 1 if unsubscire, otherwive it will be set to 0 as default
if(isset($_POST['opt-out']) && $_POST['opt-out'] == '1') {
$insert['opt_out'] = Mysql::SQLValue($_POST['opt-out']);
}
Thatâs confused me a few times in looking at this code. Not sure why the form name and field name arenât consistent, it would surely be easier.
Well, this explains the strlen() error - you can see that where everything else is being passed as a string, the opt-out field is being passed as an array. The only question now is, why? OK, this is the html code from your form, and this is why it comes in as an array: