使用小型联系表单,但是在提交/发送表单时,我收到“无数据”消息。
<form method="post" action="mail.php">
<input type="text" placeholder="Name*" name="name" required>
<input type="email" placeholder="Email*" name="email" required>
<input type="text" placeholder="Subject" name="subject">
<textarea placeholder="Message" name="message" required></textarea>
<input type="submit" value="Send" name="submit">
</form>
PHP:
<?php
// variable
$fromemail = 'any_site@my_site_com'; // from mail
$to = "marygsheehan@yahoo.ie"; // to mail
//
// check data
if (!isset($_POST["fields"])) {
die("No data");
}
$fields = $_POST["fields"];
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
$subject = "Site mail: " . $fields['subject'];
// subject massege
$subject = '=?utf-8?Q?'."\"".urlencode($subject)."\"".'?=';
$subject= str_replace("%","=",$subject);
$subject = str_replace("+","_",$subject);
// content massage
$name = $name ? $name : 'unknown';
$from = 'Mail from'."<".$fromemail.">";
$mess = $mess ? $mess : 'unknown';
$message = "<b>Client name: </b> " . $fields['name'] . "<br>";
$message .= "<b>Client email: </b> " . $fields['email'] . "<br>";
/*$message .= "<b>Client phone: </b> ".$site."<br>";*/
$message .= "<b>Subject: </b> " . $fields['subject'] . "<br>";
$message .= "<b>Text:</b>\n" . $fields['text'] . "<br>";
$message .= "Sent: ".strftime("%a, %d %b %Y %H:%M:%S");
// end content massage
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Site Mail <" . $fromemail . ">\r\n";
if(mail($to, $subject, $message, $headers)){
print 'ok';
} else {
print 'email not senta';
}
?>
我一直盯着它看,这是一个模糊,所以这可能是一个愚蠢的错误。有什么帮助表示赞赏吗?
答案 0 :(得分:1)
表单中没有名称为fields
您需要像这样直接访问它们
$_POST['your_input_name_declared_in_form']
答案 1 :(得分:1)
按照以下方式执行
if(isset($_POST['submit']))
{
$fields = $_POST;
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
}