【问题标题】:PHP checkbox not returning value on email submissionPHP复选框未在电子邮件提交时返回值
【发布时间】:2020-09-05 15:43:15
【问题描述】:

我已经尝试了this tutorial 中列出的所有内容。
但无论如何,当复选框脚本仍然产生错误消息时。

<?php
ini_set( display_errors, 1 );
error_reporting( E_ALL );
$email = $_POST['myEmail'];
$name = $_POST['myName'];
if (isset($_POST['news'])) {
    $checkBoxValue = "yes";
} else {
    $checkBoxValue = "no";
}
$to = "web@myemail.com";
$subject = "Add me to the Launch Day Notification List!";
$headers = "From:".$email;
$txt = "This person wishes to be added to the launch day notification list: $name\nDoes 
this person wish to subscribe to the newsletter? $news";
mail($to,$subject,$headers,$txt);
echo "Thank you for your inquiry, you will be added to our launch day notification 
list!";
?> 

客户端代码:

<form  action="mail.php" method="post">
    Name: <input type="text" name="myName" class="contactform" placeholder="name"required><br>
    Email: <input type="text" name="myEmail" class="contactform" placeholder="you@youremail.com" required>
    <p>Would you like to subscribe to our newsletter?</p>
    <be>
    <input type="checkbox" name="news" value="Yes" checked>
    <label for="news">Yes, I would like to subscribe</label><br>
    <input class="btns" name="btn-send" id="mySubmit" type="submit" value="submit"> 
    <input class="btns" name="btn-reset" id="myReset" type="reset" value="reset">

</form>

【问题讨论】:

  • 您也可以发布您的客户端代码吗?没有足够的信息来明确说明问题所在。
  • 如果您收到error message,那么您应该向我们展示错误消息
  • @Ynhockey 发布
  • 良好的代码缩进将帮助我们阅读代码,更重要的是它将帮助您调试您的代码Take a quick look at a coding standard为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。

标签: php forms checkbox


【解决方案1】:

您没有在输出行中使用您创建的变量$checkBoxValue,因此请使用$checkBoxValue 而不是实际不存在的$news。这可能也会消除您提到的错误。

$txt = "This person wishes to be added to the launch day notification list: $name\n
Does this person wish to subscribe to the newsletter? $checkBoxValue ";

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多