PHP脚本发送邮件但不显示表单数据 [英] PHP script sending mails but not showing form data
问题描述
我正在尝试通过联系表单发送邮件。该脚本被执行并发送邮件到我的电子邮件但无法捕获数据...它显示为空白。
I am trying to send mail through a contact form. The script is executed and send mail to my email but couldn't capture the data... it's showing blank.
这是我的HTML代码:
Here is my HTML code:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="col-sm-5 col-sm-offset-1">
<div class="form-group">
<label>Name *</label>
<input type="text" name="name" class="form-control" required="required">
</div>
<div class="form-group">
<label>Email *</label>
<input type="email" name="email" class="form-control" required="required">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label>Company Name</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label>Subject *</label>
<input type="text" name="subject" class="form-control" required="required">
</div>
<div class="form-group">
<label>Message *</label>
<textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
</div>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
</div>
</div>
</form>
这是完整的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']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'vishal@gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
实际上我正在使用HTML模板..它就在那里......我尝试了很多方法修改它但它没有捕获表单数据。
Actually I am using a HTML template.. and it was all there...I tried many ways to modify it but it's not capturing the form data.
任何建议......请检查。
Any suggestions...kindly check.
推荐答案
没有以上的事情对我有用。基础javascript中只有一个问题...它无法获取数据,序列化表单对我来说效果很好
None of the above things worked for me. There was just a problem in underlying javascript... it was not able to get the data, serializing the form worked well for me
//联系表单
var form = $('#main-contact-form');
var data = $form.serialize()
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
type: "POST",
dataType: "json",
data: data,
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">' + data.message + '</p>').delay(3000).fadeOut();
});
});
这篇关于PHP脚本发送邮件但不显示表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!