0

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?

4 Answers 4

4

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'];
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks- what should the values of prenuptial and transportation be then? I left them at yes and changed everything as you have shown, but it still is not processing
If you checked any of those value will be Yes. Otherwise it will not posted.
Can I add the value:I Will Be Attending the Prenuptial Dinner for example and then use your revised code above on the processing page?
Yes you can. And in php add like what i give answer.
I caught the error- everything is working now- thank you for your help! I cannot up vote you yet as I do not have enough reputation. But Thanks!
|
1

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.

4 Comments

Thanks- the link shows one name for both inputs and two different values. Is this what you are referring to?
Yes. The last input in the form under a given name will be the one reflected in the REQUEST.
OK all of these suggestions, I am totally confused. Do I need to have two inputs for the same option, i.e. one is hidden with a value of 1 and one is not hidden with a value of 0 like the link you posted?
First, the hidden one has the 0 value and the non-hidden has the 1 value. Second, I think you should just use the form as is and simply change the check to isset($_POST['transportation']). That does everything you want it to with far less work. If you want it to be yes/no then you add the ternary: isset($_POST['transportation'])?'yes':'no'
0

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']));

1 Comment

Thanks- I tried this- still cannot get this to process with the checkboxes added
0

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';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.