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@yahoo.com'; 
 // 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?

ianace
  • 1,646
  • 2
  • 17
  • 31
user1913714
  • 61
  • 1
  • 8

4 Answers4

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@yahoo.com';

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'];
}
Sahal
  • 4,046
  • 15
  • 42
  • 68
  • 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 – user1913714 Jan 23 '13 at 04:13
  • If you checked any of those value will be `Yes`. Otherwise it will not posted. – Sahal Jan 23 '13 at 04:19
  • 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? – user1913714 Jan 23 '13 at 04:33
  • Yes you can. And in `php` add like what i give answer. – Sahal Jan 23 '13 at 04:35
  • 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! – user1913714 Jan 23 '13 at 04:51
  • Just for the heck of it, if I wanted to just get a yes or no answer to the checkmarks, i.e. in the output it reads: Prenuptial: Yes instead of Prenuptial: I Will Be Attending the Prenuptial Dinner, how would I do that? It wasn't clear to me – user1913714 Jan 23 '13 at 04:54
  • Then your code is write. Use this `$prenuptial = (isset($_POST['prenuptial']) && $_POST['prenuptial'] == 'Yes') ? "Yes" : "No";` – Sahal Jan 23 '13 at 04:56
  • If the answer helps you, make it this is the answer :) – Sahal Jan 23 '13 at 05:09
  • Ohh thats ok, but you can't click the accept the answer also ? Just for the information I have asked. Just below the marks . – Sahal Jan 23 '13 at 05:58
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.

Community
  • 1
  • 1
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Thanks- the link shows one name for both inputs and two different values. Is this what you are referring to? – user1913714 Jan 23 '13 at 04:07
  • Yes. The last input in the form under a given name will be the one reflected in the REQUEST. – cwallenpoole Jan 23 '13 at 04:17
  • 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? – user1913714 Jan 23 '13 at 04:29
  • 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'` – cwallenpoole Jan 23 '13 at 04:31
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']));
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
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';
}
Bryan
  • 3,449
  • 1
  • 19
  • 23