我正在尝试为一个简单的表单实现php。
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test@yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
?>
<form class="form" method="post" action="say-hello.php">
<label>Name</label>
<input name="name" placeholder="Spongebob" required data-errormessage-value-missing="Whoa, you can't leave this blank!">
<label>Email</label>
<input name="email" type="email" placeholder="squarepants@krustykrab.com" required data-errormessage-value-missing="Whoa, you can't leave this blank!" data-errormessage-type-mismatch="Something isn't right...">
<label>Message</label>
<textarea name="message" placeholder="Well Hello!" required data-errormessage-value-missing="Whoa, you can't leave this blank!"></textarea>
<div class="bttnholder">
<input class="submit" name="submit" type="submit" value="Submit" placeholder="Send">
</div>
</form>
我无法弄清楚为什么我的部分PHP会以HTML格式显示,以及为什么我在页面上出现以下错误:
注意:未定义的索引:第35行的C:\ xampp \ htdocs \ sandbox \ say-hello.php中的名称
注意:未定义的索引:第36行的C:\ xampp \ htdocs \ sandbox \ say-hello.php中的电子邮件
注意:未定义的索引:第37行的C:\ xampp \ htdocs \ sandbox \ say-hello.php中的消息
有关此代码有效的任何帮助吗?
以下是第35,36,37行。
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
答案 0 :(得分:2)
您必须先检查要设置的变量。试试这个:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test@yahoo.com';
$subject = 'Hola';
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>';
}
}
答案 1 :(得分:1)
要删除通知,您应该将您的php更改为:
<?php
if(isset($_POST['name'])) {
$name = $_POST['name'];
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
$from = 'From: Hola';
$to = 'test@yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_SERVER['CONTENT_LENGTH'])) {
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>';
}
答案 2 :(得分:1)
$ _POST [&#39; submit&#39;]已过时,因为您应该检查正确的表单输入。 我检查要设置的每个表单输入,而不是为空。如果其中一个为空或未设置 - &gt;错误。 如果给出所有字段 - &gt;发送邮件。
<?php
$from = 'From: Hola';
$to = 'test@yahoo.com';
$subject = 'Hola';
if(
!isset($_POST['name']) || empty($_POST['name']) ||
!isset($_POST['email']) || empty($_POST['email']) ||
!isset($_POST['message']) || empty($_POST['message'])
){
echo '<p>Please fill in all fields</p>';
}else{
$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Message:\n " . $_POST['message'] . "";
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>';
}
}
?>
答案 3 :(得分:1)
您的邮件功能错误,其中没有“from”参数,如果您想要一个from参数,您的邮件功能应该是这样的:
首先定义标头变量:
$headers = "From: $from";
你的邮件功能:
mail($to,$subject,$body,$headers)
答案 4 :(得分:0)
您必须在if语句中插入这些行。
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Hola';
$to = 'test@yahoo.com';
$subject = 'Hola';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
加载html页面时,例如名称不存在。单击提交按钮后设置了它。因此,如果将其放在if语句中,脚本将仅在发布提交按钮后读取变量。
无论如何,为了您将来的考虑,这些不是错误,是警告,脚本仍然有效。
答案 5 :(得分:0)
以下是代码试试
$from = 'From: owais';
$to = 'test@hotmail.com';
$subject = 'Testing';
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
if(!empty($name) && !empty($email) && !empty($message) )
{
$body = "From: " . $_POST['name'] . "\n E-Mail: " . $_POST['email'] . "\n Message:\n " . $_POST['message'] . "";
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>';
}
}