【问题标题】:PHP not processing checkboxes on formPHP不处理表单上的复选框
【发布时间】:2013-01-06 11:44:34
【问题描述】:

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

这里是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");  
 ?>

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

【问题讨论】:

    标签: php html forms checkbox process


    【解决方案1】:

    为什么是strip_tags

    执行此操作,下面的代码将检查您是否选中了复选框。如果您已检查,则发送Yes 否则发送No。如果您不想发送,请将文本 No 更改为 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';
    

    看到你在两个复选框中给出的值都是yes,所以当你发布yes时,这两个复选框的值都会是。

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

    【讨论】:

    • 谢谢 - 那么婚前和交通的价值应该是什么?我把它们留在是并改变了你所展示的一切,但它仍然没有处理
    • 如果您选中了其中任何一个值,则该值为 Yes。否则不会发布。
    • 我是否可以添加值:例如,我将参加婚前晚宴,然后在处理页面上使用您修改后的代码?
    • 是的,你可以。并在php 中添加我给出的答案。
    • 我发现了错误 - 现在一切正常 - 感谢您的帮助!我还不能投票给你,因为我没有足够的声誉。不过谢谢!
    【解决方案2】:

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

    其次,你把你的父母放在了错误的位置。你有:

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

    你的意思是:

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

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

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

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

    【讨论】:

    • 谢谢-该链接显示了两个输入的一个名称和两个不同的值。你指的是这个吗?
    • 是的。表单中给定名称下的最后一个输入将反映在 REQUEST 中。
    • 好的所有这些建议,我完全糊涂了。我是否需要为同一个选项提供两个输入,即一个隐藏的值为 1,另一个不隐藏,值为 0,就像您发布的链接一样?
    • 首先,隐藏的值为0,非隐藏的值为1。其次,我认为您应该按原样使用表格并将检查更改为isset($_POST['transportation'])。这可以完成您想要的所有工作,而且工作量要少得多。如果你希望它是yes/no,那么你添加三元:isset($_POST['transportation'])?'yes':'no'
    【解决方案3】:

    改变这个:

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

    【讨论】:

    • 谢谢-我试过了-仍然无法通过添加的复选框来处理这个
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      相关资源
      最近更新 更多