0

我在表单中添加了两个复选框,设置为在发送时邮寄到电子邮件地址。听起来不错,只是它不处理表单。在我添加复选框之前,一切都处理得很好-

这是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>

过程(我删掉了很多,只留下新添加的区域-)

 $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");  
 ?>

我想知道错误是否在此处的最后一行?

4

4 回答 4

4

为什么strip_tags

这样做,下面的代码将检查您是否选择了复选框。如果您已检查,它会发送Yeselse NoNo如果您不想发送,请将文本更改为 null。

 $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 yesin both the checkbox, 所以当你发布一个 foryes将是这两个复选框的值。

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

如果你想要任何其他值,你可以把它。你可以检查使用isset

例如 - 你可以像这样添加它

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

在php中这样写

$prenuptial = '';
if (isset($_POST['prenuptial'])) {
    $prenuptial = $_POST['prenuptial'];
}
于 2013-01-23T04:04:13.010 回答
1

我看到了几件事。首先,您应该检查复选框键是否存在(如果用户不检查它们,它们将丢失)。您可以看到一种方法来确保它们始终存在。

其次,您的父母在错误的位置。你有:

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

你的意思是:

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

strip_tags第三,如果您正在检查价值,则不需要。就此而言,您甚至不需要进行平等测试。由于复选框只会在选中时出现,您可以简单地调用:

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

还应该注意的是,复选框实际上应该只用于传递 0 或 1 而不是特定的字符串值,但大多数浏览器都会让你不使用 value 属性。

于 2013-01-23T04:02:27.547 回答
0

改变这个:

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

对此:

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

或对此:

$prenuptial = !empty(strip_tags($_POST['prenuptial']));
$transportation = !empty(strip_tags($_POST['transportation']));
于 2013-01-23T04:03:45.340 回答
0

尝试检查复选框是否已设置,然后给它们一个值,即:

if(isset($_POST['transportation']) && $_POST['transportation'] == 'Yes')
{
    $transportation = 'Yes';
}
else
{
    $transportation = 'No';
}
于 2013-01-23T04:05:57.213 回答