使用mail函数时,我的PHP代码没有获取变量

时间:2017-07-04 19:57:50

标签: php forms email

我的PHP代码没有获取$ message中的变量,当使用mail函数将$ message发送到电子邮件地址时,缺少变量。不确定我做错了什么。

    <?php

    $to = 'test@email.com';
    $subject = 'Booking';

    $name = $_post['name'];
    $email = $_post['email'];
    $pickup = $_post['pickup'];
    $destination = $_post['destination'];

    $message = <<<Email
    Hi,
    Please pick up $name from $pickup and drop to $destination
    Email;

    $header = $email;


    mail($to, $subject, $message, $header);

    echo "Message Sent";

    ?>

下面提供了html代码,不确定为什么$ _POST没有通过。

<!DOCTYPE html>
<html>
<head>
    <title>Sign Up</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

    <form action="form.php" method="POST">
        <fieldset>
            <legend>Book your Journey</legend>
            <label for ="name">Name:</label>
            <input type="text" id="name" name="name">
            <label for="email">Email:</label>
            <input type="email" id="email" name="email">
            <label for="pickup"> Pick Up Location</label>
            <input type="text" id="pickup" name="pickup">
            <label>Time:</label>
            <select>
                <option>--HR--</option>
                <option value="1">01</option>
                <option value="2">02</option>
                <option value="3">03</option>
                <option value="4">04</option>
                <option value="5">05</option>
                <option value="6">06</option>
                <option value="7">07</option>
                <option value="8">08</option>
                <option value="9">09</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
            </select>
            <label>:</label>
            <select>
                <option>--MIN--</option>
                <option value="00">00</option>
                <option value="15">15</option>
                <option value="30">30</option>
                <option value="45">45</option>
            </select>
            <select>
                <option value="am">am</option>
                <option value="pm">pm</option>
            </select>
            </fieldset>
            <fieldset>
            <label for="destination"> Destination Postal Code</label>
            <input type="text" id="destination" name="destination">
        </fieldset>
        <button>Submit</button>
    </form>

</body>
</html>

3 个答案:

答案 0 :(得分:4)

The problem isn't necessarily that $message isn't picking up the variables. Rather, the problem is that $name, $email, etc. are all blank values.

PHP's variables are case sensitive, and $_POST should be all upper case.

You can do a test by outputting your $email, $name, etc. and you'll see that they're blank.

To fix it, all you need to do is capitalize $_POST:

$name = $_POST['name'];
$email = $_POST['email'];
$pickup = $_POST['pickup'];
$destination = $_POST['destination'];

答案 1 :(得分:1)

区分大小写......

我还建议使用:filter_input_array();

您可以按如下方式使用它:

<?php
$input = filter_input_array(INPUT_POST);

$to      = 'test1@gmail.com';
$subject = 'Booking';

$name        = $input['name'];
$email       = $input['email'];
$pickup      = $input['pickup'];
$destination = $input['destination'];

您也可以将它与GET方法一起使用,如下所示:

filter_input_array (INPUT_GET);

不需要使用?>

答案 2 :(得分:0)

总是大写。$ _ POST [&#39;变量名称&#39;]然后尝试。

在$ _POST实现无效后尝试此操作:

document.getElementById("your-id").click = function () {
    newButton_Click();
}