php中的Xampp错误(定义变量?)

时间:2013-10-24 13:07:44

标签: php html xampp

在我试图解决我的问题2天后,我放弃了:(所以会尽力在这里寻求帮助。 我的问题是xampp给了我这个错误:

  

注意:未定义的索引:C:\ xampp \ htdocs \ portfolio \ index.php中的名称   在第78行注意:未定义的索引:电子邮件在   第79行的C:\ xampp \ htdocs \ portfolio \ index.php注意:未定义   index:第80行的C:\ xampp \ htdocs \ portfolio \ index.php中的消息   注意:未定义的索引:C:\ xampp \ htdocs \ portfolio \ index.php中的human   第84行注意:未定义的索引:提交   第88行的C:\ xampp \ htdocs \ portfolio \ index.php

我设法发现我需要在使用它们之前定义变量,或检查是否存在,但我对PHP的了解非常基础。 有人可以帮我吗?

<section id="contact">
<h3>Contact me :</h3>
<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = 'example@gmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

<form method="post" action="index.php">

    <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>
    <label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">

    <input id="submit" name="submit" type="submit" value="Submit">

</form>


</section>

2 个答案:

答案 0 :(得分:0)

由于没有 POST 请求, $ _ POST ['name'] 等变量不可用,因此 PHP 会给出错误。您需要做的是检查是否已发出 $ _ POST 请求,然后定义变量:

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Example'; 
    $to = 'example@gmail.com'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '') {
        if ($human == '4') {                 
            if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>'; 
        } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly!</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
}
?>

答案 1 :(得分:0)

目前,您的页面显示这些消息,因为您显示了所有错误和通知。

如果您在页面顶部包含以下内容,则不会看到通知消息:

<?php error_reporting (E_ALL ^ E_NOTICE);  ?>

但请注意,这只会隐藏消息。它无法解决根本问题。