开发者

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

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 h开发者_运维问答ere' 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>


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?


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜