Comments Form - email newsletter opt-in

rl2rl2
edited November 2017 in Share
Hello,

In blog/libraries/Fuel_log.php I have added the below for rendering to the page.

$fields['subscribed'] = array('label' => 'Would you like to receive our newsletters?', 'type' => 'checkbox', 'class' => 'form-control', 'checked' => true, 'value' => 1);

And in models/Blog_comments_model.php added 'subscribed' to the array of hidden fields so that cms administrators cannot manually over-ride the opt in and out.

The checkbox always saves to the database as 1 (the checked state).

How do I write to the database as 0 or 1 depending on whether the checkbox is checked or unchecked?

Comments

  • edited 10:32AM
    What's likely happening is that if it's unchecked, it will not be passed at all in the $_POST array. If it's not seen in the $_POST, then it won't trigger it to be 0. To help with this issue, there is a public $boolean_fields property you can set on the Blog_comments_model to help trigger a 0 if it's not seen on the $_POST:
    public $boolean_fields = array('subscribed');
  • rl2rl2
    edited November 2017
    Okay tried that, didn't work. :( In my database the subscribed column Data Type is set to Tinyint, Length 1, Default '', Not Null yes.

    Previously I had Default set to 1, I now have set this to 0 - and have observed that it always writes 0 - never 1.
  • edited 10:32AM
    Actually in your case, you'll need to make an adjustment to the fuel/modules/blog/controllers/Blog::_process_comment method probably around line 307.
  • rl2rl2
    edited November 2017
    Thank you, this worked form me.

    $comment->subscribed = $this->input->post('subscribed', TRUE);
    if ( empty( $comment->subscribed) ) { $comment->subscribed = 0; }
Sign In or Register to comment.