注意:未定义的索引:错误

时间:2016-08-11 19:22:53

标签: php html forms

我正在尝试通过网络表单发送电子邮件,但我得到了这个未定义的索引错误

 Undefined index: email in C.....
 Undefined index: phone in C....,
 Undefined index: name in C......
 Undefined index: message in C.....

下面是我的PHP代码

<?php
session_start();

//error_reporting(0);

if(isset($_POST['submitted'])) { 
 {
   $sendto = "email@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $name . " " .  " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $sendto;

    $msg  = "<html><body style='font-family:Arial,sans-serif;'>";
    $msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
    $msg .= "<p><strong>Sent by:</strong> ".$from."</p>\r\n";
    $msg .= "<p><strong>Message:</strong> ".$message."</p>\r\n";
    $msg .= "<p><strong>Name:</strong> ".$name."</p>\r\n";
    $msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n";
    $msg .= "</body></html>";


    if(mail($sendto, $subject, $msg, $headers)) {

        echo '<center><p style="color:green">Mail Sent. Thank you, we will contact you shortly.</p></center>';

    } else {

        echo '<center><p style="color:red">Mail not Sent. Try Again.</p></center>';
    }
}

}
?>

但是当我提交表单时,它会抛出这些错误 以下是我的表格

<form name="submitted"action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
    <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
        <input type="text" id="name" required />
        <label>Name</label>
        <span></span>
    </div>
    <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
        <input type="email" id="email"required />
        <label>Email</label>
        <span></span>
    </div>
    <div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
        <input type="tel" id="phone"required />
        <label>Phone</label>
        <span></span>
    </div>
    <div class="styled-input wide wow slideInUp animated animated" data-wow-delay=".5s">
        <textarea id="message" required></textarea>
        <label>Message</label>
        <span></span>
    </div>
    <div class="send wow shake animated animated" data-wow-delay=".5s">
        <input name="submitted" type="submit" value="Send" >
    </div>
</form>

如何修复此错误? 欢迎任何帮助

1 个答案:

答案 0 :(得分:2)

改变这个:

<input type="text" id="name" required />

对此:

<input type="text" id="name" name="name" required />

对没有<input>属性的所有<textarea>代码(和name)重复此操作。需要name个属性才能发送表单数据并在$_POST中提供。