【问题标题】:Simple PHP Forms to Email - Not relaying input information简单的 PHP 表单到电子邮件 - 不中继输入信息
【发布时间】:2015-09-01 22:07:58
【问题描述】:

我正在尝试将表单输入框收集的信息发送到电子邮件。电子邮件正在发送,但电子邮件消息未显示在输入框中输入的信息。

(这就是我得到的)

姓名:

编号:

地点:

电子邮件:

我是 PHP 新手,任何建议都将不胜感激!

PHP

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$phone= $_POST['phone'];
$location= $_POST['location'];

$formcontent = "
Name: $name \n
Number: $phone \n
Location: $location \n
Email: $email";

$recipient = "chris@knorthstudios.com";
$subject = "Boston Calling Street Team Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you";

?>

表格代码:

 <form method="POST" action="contact.php">
     label>Name:</label>
     <input type="text" class="inputbox" name="name" required>

     <label>Age:</label>
     <input class="inputbox" name="age" required>

     <label>Location (City/State): </label>
     <input class="inputbox" name="location" required>

     <label>Phone number:  </label>
     <input class="inputbox" name="phone" required>                        

     <label>E-mail address:</label>
     <input class="inputbox" name="email" required>

     <label>What is your favorite flavor of icecream</label>
     <textarea class="inputbox textinputbox"  name="icecream" required></textarea>

     <input class="streetteamsubmit" type="submit" name="submit" value="SUBMIT FORM">

 </form>

【问题讨论】:

  • 完成任何基本调试,例如var_dump($_POST, $formcontent) 以查看您收到的内容和构建的内容?

标签: php email post input


【解决方案1】:

a) 开启错误报告。在代码顶部添加:

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

b) php 代码和表单是否在同一个文件中?

然后您应该检查该页面是否已通过 POST 方法发送。访问者第一次到达联系表总是在 GET 方法中。 (他刚刚到达页面,仍然需要填写表单域)

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // visitor has sent the form
}

c) 你永远不知道 $_POST 元素是否真的存在,因此你应该使用 isset() 函数检查它

// initializing with a default value
$name = '';

// overwrite value if $_POST element exists
if(isset($_POST['name']))
{
    $name = $_POST['name'];
}

d) 在这里,您将看到一个非常基本的带有验证的良好工作表单示例。

<?php

// Function that validates your formfields and generate errors
function validate($naam, $email)
{
    $errors = array();

    // validation for name
    if(strlen($naam) < 2)
        $errors[] = 'U heeft geen naam ingevuld.';

    // validation for email
    if(!strlen($email))
        $errors[] = 'U heeft geen email adres ingevuld.';
    else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        $errors[] = 'U heeft een ongeldig email adres ingevuld.';

    // return array with errors
    return $errors;
}

// initialization of variables with default values
$naam = '';
$email = '';
$errors = array();

// if the form has been submited
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // overwrite the variables with the $_POST elements
    $naam = $_POST['naam'];
    $email = $_POST['email'];

    // validate the variables
    $errors = validate($naam, $email);

    // If there aren't any errors redirect to another page
    if(!count($errors))
    {
        // Here comes the place to send an email!!!

        header('Location: thankyou.html');
        exit;
    }
}
?>
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>a titel</title>
    </head>
    <body>
        <ul id="errors">
        <?php
            foreach($errors as $error)
            echo '<li>' . $error . '</li>';
        ?>
        </ul>
        <form action="" method="post">
            <input type="text" name="naam" value="<?php echo $naam; ?>" />
            <input type="email" name="email" value="<?php echo $email; ?>" />
            <button type="submit">Verzenden</button>
        </form>
    </body>
</html>

【讨论】:

  • 感谢您花时间写这篇文章。我在提交时收到了电子邮件,但我不确定如何在语法中使用 $name 和 $email:我正在使用 // 用 $_POST 元素覆盖变量 $name = $_POST['name ']; $email = $_POST['email']; $recipient = 'chris@gmail.com'; $subject = '团队形式'; $mailheader = '发件人:$email \r\n'; $formcontent = '$name, $email'; if(!count($errors)) mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); header('位置:contact.php');出口; }
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2014-10-24
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多