tags:

views:

61

answers:

2

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!

Here's the full form minus the option list for the sake of brevity

<form method="post" action="contact.php" name="contactform" id="contactform">

        <fieldset>

        <legend>Please fill in the following form all fields are required, thanks!</legend>

        <label for=firstName accesskey=F><span class="required">*</span>First Name</label>
        <input name="firstName" type="text" id="firstName" size="30" value="" /> 

        <br />
        <label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
        <input name="lastName" type="text" id="lastName" size="30" value="" /> 

        <br />
        <label for=email accesskey=E><span class="required">*</span>Email</label>
        <input name="email" type="text" id="email" size="30" value="" />

        <br />
        <label for=city accesskey=C><span class="required">*</span>City</label>
        <input name="city" type="text" id="city" size="30" value="" /> 

        <br />


        <label for=state accesskey=S><span class="required">*</span>State</label>
        <select name="state" type="text" id="state">
            <option value="AL">Alabama</option> 
            ...
            <option value="WY">Wyoming</option>
        </select>

        <br />
        <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;" /> 

        <br />           
        <p><span class="required">*</span> Are you human?</p>

        <label for=verify accesskey=V>&nbsp;&nbsp;&nbsp;3 + 1 =</label>
        <input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />

        <input type="submit" class="submit" id="submit" value="Submit" />

        </fieldset>

        </form>
A: 

This has just dawned on me.

If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.

Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.

Edit 2: I've tested this to death. Change your code to:

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

Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".

Danten
Isn't that exactly what his code does?
Michael Mrozek
No, it's checking if the checkbox is set to 'yes', as it never will be. Checkboxes post on or it isn't set at all.
Danten
I do not agree. The value of a checkbox field is seen in $_POST. You are correct that if it is not checked no key or value will be seen in $_POST.
erisco
What erisco said. Furthermore, his problem is the condition appears to always be true, not false
Michael Mrozek
I have just tested this in Firefox and IE8 - it never receives the value attribute, just a string reading 'on'.
Danten
some browsers just pass 'on', some pass the value. im on ff3.6.3 and i just tested it. it passed the value.
Galen
Thank you guys very much for the help! turns out it was a stupid js mistake! I appreciate the time you put into looking into the error
Ryan
+2  A: 

Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?

dkamins
Well the database insert works, it's just always inserting as true, so even if people don't want our newsletter it is being stored in the DB as if they do.
Ryan
@Ryan Maybe try `var_dump($_POST)` at the beginning to make sure newsletter is showing up like you think? dkamins is right, your code looks fine
Michael Mrozek
Wow I feel foolish now; it was a javascript oversight. I forgot the :checked in newsletter: $('#newsletter:checked').val(),so whether it was checked or not jquery was sending the php that there was indeed a checkbox field so always evaluating true :/Thank you everyone for taking the time to check it out and for the help! I probably would have kept racking my brain against the php instead of checking the js!
Ryan