HTML checkbox field is being passed to PHP as checked even when it is not

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2010-04-14T01:59:51Z Indexed on 2010/04/14 2:03 UTC
Read the original article Hit count: 328

Filed under:
|
|

Hello all, First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.

I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:

<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
        <input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />

That is then submitted to a php file with this code:

    if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
     echo "newletter yes";
     $newsletter = 1;
     }else{
        echo "newsletter no";
        $newsletter = 0;
        }

$newsletter is then inserted into a database field.

The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.

Any help would be greatly appreciated! Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql