这实际上是我在这里发表的第一篇文章。可能是分号问题,但我会试一试,因为我的headeache比埃菲尔铁塔大。
所以我在我的网站上添加了一个联系表单,但是它有问题,因为它正在发送电子邮件,但是当我收到它时它是空的。它只显示PHP文件中的文本(名称:,电子邮件:等),但没有来自输入表单的提示。
代码如下所示: 的 HTML
<div class="contact-form bottom">
<h2>Send a message</h2>
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" required class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
PHP :
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'a.truta@icloud.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
请帮助,我在这里疯了!
答案 0 :(得分:1)
正如Marc所说,你应该在表格上做一些验证。这是我很快就会为你敲门的东西。它并不完美,我没有机会测试它,但它会给你一个更好的表格处理起点。不确定json_encode
在您的代码中的相关性。
<?php
$toemail = "you@email.address";
if (isset($_POST)) {
$errors = [];
// Validate name
if ((isset($_POST['name'])) and (preg_match("/^[a-zA-Z ]+$/", $_POST['name']))) {
$name = $_POST['name'];
} else {
$errors[] = "Name is invalid";
}
// Validate email
if ((isset($_POST['email'])) and (filter_var($email, FILTER_VALIDATE_EMAIL))) {
$email = $_POST['email'];
} else {
$errors[] = "The email is invalid";
}
// Validate message
if ((isset($_POST['message'])) and (preg_match("/^[0-9a-zA-Z _-]+$/", $_POST['message']))) {
$message = $_POST['message'];
} else {
$errors[] = "The message is invalid";
}
if ((isset($name)) and (isset($email)) and (isset($message)) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <' . $email . '>' . "\r\n";
if(@mail($toemail, 'New contact form email', $message, $headers)) {
echo "<p>Mail Sent Successfully</p>\n";
} else {
echo "<p>Mail Not Sent Successfully</p>\n";
}
} else {
echo "<p>Form validation has failed</p>";
foreach ($errors as $error) {
echo "<p>" . $error . "</p>\n";
}
}
}
?>
答案 1 :(得分:0)
嗯,显然问题出在JS文件中。如果没有此代码,电子邮件将准确到达其发送方式,包含内容和内容。所有
JS文件:
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});