未定义索引$ _POST php联系表单

时间:2014-03-27 15:11:34

标签: php html html5 contact-form

请有人帮助我。我有这个代码,我不知道为什么我有这些错误。

"Undefined index name, email and message"

我试图在表单中添加一个动作:

<form action="?" method="post">但不起作用。

<?php
$to = 'myemail@something.else';
$subject = 'From Site';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$header = '$email';

if ($_POST) {
    mail($to, $subject, $message, $header);
}
?>

<html>
    <fieldset>
        <legend>Contactat US</legend>
        <form method="post">
            <label for="name">Name:</label><br>
            <input type="text" name="name" id="name" /><br>
            <label for="email">E-mail:</label><br>
            <input type="text" name="email" id="email" /><br>               
            <label for="message">Message:</label><br>
            <textarea id="message" name="message" id="message" cols="42" rows="9"></textarea><br><br>
            <input type="submit" value="Send" />                
        </form> 
    </fieldset>
</html>

2 个答案:

答案 0 :(得分:2)

if(isset($_POST['email'])){
    $to = 'myemail@something.else';
    $subject = 'From Site';

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $header = '$email';

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

答案 1 :(得分:0)

替换为:

 if(isset($_POST['email'])){
        $to = 'myemail@something.else';
        $subject = 'From Site';

        $name = filter_input(INPUT_POST, 'name');
        $email = filter_input(INPUT_POST, 'email');
        $message = filter_input(INPUT_POST, 'message');

        $header = '$email';

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

如果直接查询post数组,可能会遇到困难。 你看到一些编辑发出警告。 (例如Netbeans IDE)