尝试获取一个AJAX联系表单来发送电子邮件,但它已经无法正常工作,经过充分的研究,我仍然无法解决错误,所以也许你们中的一个可以提供帮助
JS:
// Contact form
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();
});
});
PHP
<?php
if(isset($_POST['submit'],$_POST['name'],$_POST['email'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $email;
$email_to = 'email@email.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . "\n\n" . 'Message: ' . $message;
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
$response = array(); }
echo json_encode($response);
die;
?>
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" id="name" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" id="email" name="email" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="message" id="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>
请帮忙!我没有选择。
答案 0 :(得分:0)
ajax函数缺少数据参数。
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: { // pass the data values sending through ajax
name : name,
email : email,
message : message
},
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();
});
});
您可以通过ajax获取要通过php脚本发送的值,如下所示:
var name = $('#name').val();
var email = $('#email').val();
var message = $('#message').val();
获得这些值后,您可以调用ajax函数将这些值作为数据参数传递,并将数据类型设置为POST
。然后在PHP脚本中,您可以完全像您一样获得帖子值。
答案 1 :(得分:0)
$ subject初始化了哪里?在将它发送到邮件功能之前,我没有看到它在PHP代码中获得价值
答案 2 :(得分:0)
要发送电子邮件,您需要配置类似的内容,您也需要使用 要启用的安全性较低的应用程序(如果是Gmail)。为了让您更安全 需要登录您的帐户,然后点击以下链接:
public function sendEmail($email, $subject, $message) {
$config= array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => 'example@gmail.com',
'smtp_pass' => 'xxxxxxx',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->initialize($config);
$this->email->from('localhost', 'Team Name');
$this->email->to($email);
$this->email->subject($subject);
$this->email->message($message);
if(!$this->email->send()){
echo $this->email->print_debugger();
}
}