我是php新手,我发了一个联系表,但我得到了
注意:未定义的索引:名称在 C:\ XAMPP \ htdocs中\组合\组合\ 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>
答案 0 :(得分:3)
<?php
$name = (isset($_POST['name']) ? $_POST['name'] : '');
$email = (isset($_POST['email']) ? $_POST['email'] : '');
$message = (isset($_POST['message']) ? $_POST['message'] : '');
?>
答案 1 :(得分: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'];
}
答案 2 :(得分:0)
将上面的代码放在测试块中,如下所示
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
...
until the end of ?>
这是因为在没有表单提交的情况下加载页面。 (因为你在同一个文件中有控制逻辑和显示逻辑)它会执行顶部的代码。