PHP:联系表单错误未定义的索引和变量

时间:2015-05-27 06:18:14

标签: php html css

请帮助解决这个问题,我尝试了所有的决议。我是php的初学者,对修复bug知之甚少。我只是复制粘贴此代码,现在我得到未定义索引和变量的错误。喜欢这个 - "注意:未定义的变量:第27行和第34行的C:\ xampp \ htdocs \ magpie \ index1.php中的errName; 等等.... 如果您有完整的PHP联系表单代码,那么也发布。提前谢谢..

    <?php
    if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'contact test'; 
    $to = 'example@domain.com'; 
    $subject = 'Message from Contact Demo ';

    $body ="From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
error_reporting (E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
            <h1 class="page-header text-center">Contact Form Example</h1>
            <form class="form-horizontal" role="form" method="post" action="index1.php">
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                        <?php echo "<p class='text-danger'>$errName</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                        <?php echo "<p class='text-danger'>$errEmail</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="message" class="col-sm-2 control-label">Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                        <?php echo "<p class='text-danger'>$errMessage</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php echo $result; ?>  
                    </div>
                </div>
            </form> 
        </div>
    </div>
    </div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>

请发布任何有效的联系表格代码,或者如果您在上述代码中发现错误,请在答案中发帖。非常感谢提前...我是一个photoshop,coreldraw和插画设计师,知道点网的小编码,但是在php中的begginer。

7 个答案:

答案 0 :(得分:2)

错误只是说你有一个你没有定义的变量。 例如:

//Some code over here
...
...
echo $bla;

由于$bla未定义且没有价值 - 您将收到有关此事的通知。

在HTML表单中,您正在使用这些$ err变量而不定义它们。 乍一看,你会说,我定义了提到的变量,例如:

$errMessage = 'Please enter your message';

但是,仔细看看,你已经在某种条件下定义了它们,例如:

if (!isset($_POST['message'])) {

因此,如果表单尚未提交 - 那些变量并未真正定义。

解决方案1:初始值

在页面顶部,只需定义您要使用的所有变量,并设置默认/空值。

$errBla = '';
$errBla2 = '';

顺便说一下,我更倾向于使用数组来表示“错误”状态。

解决方案2:isset isset函数允许您检查是否已定义变量。 所以没有:

<?php echo "<p class='text-danger'>$errMessage</p>";?>

if (!$errName && !$errEmail && !$errMessage) {

你将拥有:

<?php echo (isset($errMessage) ? "<p class='text-danger'>$errMessage</p>" : ''; ?>

if ((isset($errName) && !$errName) && (isset($errEmail) && !$errEmail) ... )

答案 1 :(得分:0)

您的$ errMessage仅在您没有消息时设置。与$ errName和$ errEmail相同。要解决这个问题,您需要使用isset:

if (!isset($errName) || !isset($errEmail) || !isset($errMessage)) {

我也将其更改为或(||),否则无论您设置错误消息,脚本都将继续执行。此代码段检查是否存在错误消息,如果只设置了错误消息则显示错误。

答案 2 :(得分:0)

这不是错误,它只是一个通知,变量尚未设置,但你的主要问题是你正在使用xampp你应该考虑实现这个 CLICK this LINK

我相信你只是复制你的源代码,我相信它也有效,所以我想你应该首先从你的xampp启用你的电子邮件服务器或测试它在线服务器。

答案 3 :(得分:0)

要检查PHP中是否存在变量,您应该使用isset()函数。如果变量已设置且不为null,则返回true。

!返回它前面的布尔变量的反面。

而不是if(!$ errName)使用if(isset($ errName))。

答案 4 :(得分:0)

请在下面找到解决方案:     

    $body ="From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errName = 'Please enter your name';
    } else {
        $errName = '';  
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    } else {
        $errEmail = ''; 
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    } else {
        $errMessage = '';   
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
            }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
            <h1 class="page-header text-center">Contact Form Example</h1>
            <form class="form-horizontal" role="form" method="post" action="index1.php">
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">Name</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                        <?php echo "<p class='text-danger'>$errName</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email" class="col-sm-2 control-label">Email</label>
                    <div class="col-sm-10">
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                        <?php echo "<p class='text-danger'>$errEmail</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <label for="message" class="col-sm-2 control-label">Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                        <?php echo "<p class='text-danger'>$errMessage</p>";?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php echo $result; ?>  
                    </div>
                </div>
            </form> 
        </div>
    </div>
    </div>   
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>

答案 5 :(得分:0)

检查一下我会解决问题......

  <?php
if (isset($_POST["submit"])) {
    $name    = $_POST['name'];
    $email   = $_POST['email'];
    $message = $_POST['message'];
    $from    = 'contact test';
    $to      = 'example@domain.com';
    $subject = 'Message from Contact Demo ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage) {
        if (mail($to, $subject, $body, $from)) {
            $result = '<div class="alert alert-success">Thank You! We will be in touch.</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
}
error_reporting(E_ALL ^ E_NOTICE);
?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                <h1 class="page-header text-center">Contact Form Example</h1>
                <form class="form-horizontal" role="form" method="post" action="index1.php">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">Name</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php
echo htmlspecialchars($_POST['name']);
?>">
                            <?php
if (!isset($_POST['name'])) {
    echo "<p class='text-danger'>$errName</p>";
}


?>
                       </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php
echo htmlspecialchars($_POST['email']);
?>">
                            <?php
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    echo "<p class='text-danger'>$errEmail</p>";
}


?>
                       </div>
                    </div>
                    <div class="form-group">
                        <label for="message" class="col-sm-2 control-label">Message</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" rows="4" name="message"><?php
echo htmlspecialchars($_POST['message']);
?></textarea>
                            <?php
if (!isset($_POST['message'])) {
    echo "<p class='text-danger'>$errMessage</p>";
}

?>
                       </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-10 col-sm-offset-2">
                            <?php
echo $result;
?>  
                        </div>
                    </div>
                </form> 
            </div>
        </div>
        </div>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
    </html>

答案 6 :(得分:-1)

请更改您的if条件。见下面的代码:

// If there are no errors, send the email

if(!isset($errName) || !isset($errEmail) || !isset($errMessage)) {   // Change this line
if (mail ($to, $subject, $body, $from)) {
    $result='<div class="alert alert-success">Thank You! We will be in touch.</div>';
} else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }