简体   繁体   English

PHP未定义索引错误?

[英]PHP undefined index error?

I have a simple contact form thats throwing up an 我有一个简单的联系表,那就是

"Notice: Undefined index: e in \\nas43ent\\Domains\\m\\mysite.co.uk\\user\\htdocs\\contact-us.php on line 24" “通知:未定义的索引:第24行的\\ nas43ent \\ Domains \\ m \\ mysite.co.uk \\ user \\ htdocs \\ contact-us.php中的e”

error, I cant seem to see why however as my syntax looks correct? 错误,但是我似乎看不出为什么语法正确?

<?php 

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "dave@.co.uk";

    //begin of HTML message
    $message = "
  From : $name,
  Email: $email,
  Subject: $subject,
  Message: $message ";
   //end of message

    // To send the HTML mail we need to set the Content-type header.
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers  .= "From: Website Enquiry";


if (isset($_POST['name'])) {
       // now lets send the email.
   mail($to, $subject, $message, $headers);

header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');

} else {header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');}

?> 

Looks like it is related to e get parameter. 看起来它与e get参数有关。 After sending email you redirect user to some url like http://yourhost/com/somepage?e=Error+text 发送电子邮件后,您将用户重定向到一些URL,例如http:// yourhost / com / somepage?e = Error + text

So the notice you get seems to be related to the place you display status message. 因此,您收到的通知似乎与您显示状态消息的位置有关。 You should put check there (as by default you don't have e in your query string): 您应该在此处放置检查(默认情况下,查询字符串中没有e ):

if (isset($_GET['e'])) {
    //output message 
}

In place where you read $_GET['e'] paramter you should first check if it's present with isset : 在读取$_GET['e']参数的地方,应首先检查isset是否存在:

if (isset($_GET['e'])) {
    // show the message to user
}

This is not an error but a NOTICE this is really different. 这不是错误,但请注意,这确实有所不同。

If $_SERVER['HTTP_REFERER'] is not set, you will receive this notice. 如果未设置$ _SERVER ['HTTP_REFERER'],您将收到此通知。

You should add if(isset($_SERVER['HTTP_REFERER'])) . 您应该添加if(isset($_SERVER['HTTP_REFERER']))

Try the following 尝试以下

if (isset($_POST['name']) AND isset($_SERVER['HTTP_REFERER'])) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM