2

我是 php 新手,我制作了一个联系表格,但我得到了一个

注意:未定义索引:名称在 C:\xampp\htdocs\portfolio\portfolio\html\contact.php

对于这些行$_POST- 姓名、电子邮件、消息和人员。

我怎样才能消除错误?

                                    $name = $_POST['name'];
                                    $email = $_POST['email'];
                                    $message = $_POST['message'];
                                    $from = 'From:'; 
                                    $to = '86376@ict-idcollege.nl'; 
                                    $subject = 'Hello';
                                    $human = $_POST['human'];

                                    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
                                        if (isset($_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="contact.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>*Wat is 2+2? (Anti-spam)</label>
                                    <input name="human" placeholder="Type Here">

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

                                </form>
4

3 回答 3

3

通过使用isset!empty

<?php
    $name    = (isset($_POST['name'])    ? $_POST['name']    : '');
    $email   = (isset($_POST['email'])   ? $_POST['email']   : '');
    $message = (isset($_POST['message']) ? $_POST['message'] : '');
?>
于 2013-08-20T09:51:31.483 回答
1

只需在您的代码中进行更改:

if (isset ($_POST['name'])) {
  $name = $_POST['name'];
}
if (isset ($_POST['email'])) {
  $email = $_POST['email'];
}
if (isset ($_POST['message'])) {
  $message = $_POST['message'];
}

$from = 'From:';
$to = '86376@ict-idcollege.nl';
$subject = 'Hello';

if (isset ($_POST['human'])) {
  $human = $_POST['human'];
}
于 2013-08-20T09:52:30.170 回答
0

将上面的代码放在测试块中,如下所示

 if (isset($_POST['submit'])) {
   $name = $_POST['name'];
   $email = $_POST['email'];
  ...
  until the end of ?>

这是因为当页面加载时没有提交表单。(因为您在同一个文件中有控制逻辑和显示逻辑)它执行顶部的代码。

于 2013-08-20T09:52:17.787 回答