user1913714
user1913714

Reputation: 61

PHP not processing checkboxes on form

I have added two checkboxes to a form which are set to be mailed to an email address on send. Sounds great, only it is not processing the form. Everything was processing great until I added the checkboxes-

Here is the HTML:

<form id="form" method="post" name="validation" action="index.php" onsubmit="received()">
    <fieldset>
        <label for="name">Full Name:</label>
        <input type="text" name="name" title="Enter your name">

        <label for="attending"># Attending:</label>
        <input style="margin-left:190px;" type="text" name="attending" title="Optional">

        <label for="guests">Name of Guest(s): </label>
        <input style="margin-left:370px;" type="text" name="guests">

        <span class="fakelabel">Please Check All that Apply:</span>

        <input type="checkbox" name="prenuptial" value="Yes"><span class="additions">I Will Be Attending the Prenuptial Dinner</span><br>

        <input type="checkbox" name="transportation" value="Yes"><span class="additions">I Will Be Requiring Transportation To and From the Wedding</span>

        <div class="submitcontainer">
            <input type="submit" style="font-size:0;" name="submit" class="submitbutton" id="submit">
    </fieldset>
</form>

Process (I cut a lot out leaving only the newly added areas-)

 $name = strip_tags($_POST['name']);
 $attending = strip_tags($_POST['attending']);
 $guests = strip_tags($_POST['guests']);
 $prenuptial = strip_tags($_POST['prenuptial'] == 'Yes');
 $transportation = strip_tags($_POST['transportation'] == 'Yes');
 $to = '[email protected]'; 
 // Send Message

 mail($email, "RE: Wedding RSVP", $intro, $headers); 
 mail($to, "Wedding RSVP", "Name: {$name}\n Attending: {$attending}\n Guests: {$guests}\n Prenuptial: {$prenuptial}\n Transportation: {$transportation}\n");  
 ?>

I'm wondering if the error is in the last line here?

Upvotes: 0

Views: 660

Answers (4)

Sahal
Sahal

Reputation: 4136

Why strip_tags ?

Do this, Below code will check whether you have selected checkbox or not. If you have checked, it send Yes else No. Change text No to null if you don't want to sent.

 $name = strip_tags($_POST['name']);
 $attending = strip_tags($_POST['attending']);
 $guests = strip_tags($_POST['guests']);
 $prenuptial = (isset($_POST['prenuptial']) && $_POST['prenuptial'] == 'Yes') ? "Yes" : "No";
 $transportation = (isset($_POST['transportation']) && $_POST['transportation'] == 'Yes') ? "Yes" : "No";
 $to = '[email protected]';

See You have given value is yes in both the checkbox, So when you post a for yes will be the value of both the checkbox.

<input type="checkbox" name="prenuptial" value="Yes"><span class="additions">I Will Be Attending the Prenuptial Dinner</span><br>
<input type="checkbox" name="transportation" value="Yes"><span class="additions">I Will Be Requiring Transportation To and From the Wedding</span>

If you want any other value you can put it. And you can check using isset

Eg - you can add it like this

<input type="checkbox" name="prenuptial" value="I Will Be Attending the Prenuptial Dinner"><span class="additions">I Will Be Attending the Prenuptial Dinner</span><br>  
<input type="checkbox" name="transportation" value="I Will Be Requiring Transportation To and From the Wedding"><span class="additions">I Will Be Requiring Transportation To and From the Wedding</span>

And in php write like this

$prenuptial = '';
if (isset($_POST['prenuptial'])) {
    $prenuptial = $_POST['prenuptial'];
}

Upvotes: 4

cwallenpoole
cwallenpoole

Reputation: 82028

There are a couple of things that I see. First you should be checking to see if the checkbox keys are present (they will be missing if the user does not check them). You can see one way to make sure that they are always there here.

Second, you have your paren's in the wrong spot. You have:

strip_tags($_POST['transportation'] == 'Yes');

You meant:

strip_tags($_POST['transportation']) == 'Yes';

Third, you don't need strip_tags if you're checking for the value. For that matter, you don't even need a test for equality. Since checkboxes will only appear when checked, you can simply call:

$transpertation = isset($_POST['transportation']);

It should also be noted that checkboxes really should only be used for passing 0 or 1 and not specific string values, but most browsers will let you get away with using the value attribute anyway.

Upvotes: 1

Bryan
Bryan

Reputation: 3494

try checking if the check boxes are set then giving them a value ie:

if(isset($_POST['transportation']) && $_POST['transportation'] == 'Yes')
{
    $transportation = 'Yes';
}
else
{
    $transportation = 'No';
}

Upvotes: 0

Giacomo1968
Giacomo1968

Reputation: 26066

Change this:

$prenuptial = strip_tags($_POST['prenuptial'] == 'Yes');
$transportation = strip_tags($_POST['transportation'] == 'Yes');

To this:

$prenuptial = strip_tags($_POST['prenuptial'] == TRUE);
$transportation = strip_tags($_POST['transportation'] == TRUE);

Or to this:

$prenuptial = !empty(strip_tags($_POST['prenuptial']));
$transportation = !empty(strip_tags($_POST['transportation']));

Upvotes: 0

Related Questions