尝试在Codeigniter中设置AJAX联系表单。这是我第一次使用CI。
我有它工作,现在它已经坏了,我不知道为什么。
这是代码..
JS
$.ajax({
type:'POST',
url: post_url,
data: post_data,
beforeSend:function(){
alert(this.data); <<< This is alerting the correct form data to me
},
success:function(msg){
$($form).fadeOut(500, function(){
var msg = '<div class="messageSent"><p>'+ msg +'</p></div>';
$form.html(msg).fadeIn(); <<< This is returning undefined
});
}
});
..和PHP ......
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->library('email');
}
public function index(){
$emailFromName = $this->input->post('name', TRUE);
$emailFromAddress = html_entity_decode($this->input->post('email', TRUE));
$emailFromMessage = "Email from the Brewer's website\n";
$emailFromMessage .= "==============================\n";
$emailFromMessage .= "From: " + $emailFromName + "\n";
$emailFromMessage .= "==============================\n";
$emailFromMessage .= "Respond Email; " + $emailFromAddress +"\n";
$emailFromMessage .= "==============================\n";
$emailFromMessage .= $this->input->post('message', TRUE);
$this->email->from($emailFromName);
$this->email->to('recipient@gmail.com');
$this->email->subject('Message from the website.');
$this->email->message($emailFromMessage);
$this->email->send();
echo $this->email->print_debugger(); // echoes undefined
// echo($emailFromName); // also echoes undefined
}
}
?>
我认为问题必须在PHP中..它似乎似乎没有读取任何发布的变量。我甚至无法回复$ this-&gt; input-&gt; post('name'); (虽然我有它工作!)
..所以我做错了什么?
注意 - 我把html_entity_decode放在那里,因为我注意到电子邮件addy被发布为“地址%40gmail.com”...所以这似乎是一个好主意。有必要吗?
答案 0 :(得分:0)
严格地说,这不是你问题的答案 - 但我在稳定CodeIgniter项目的邮件功能方面做了很多努力。我找到的解决方案花了大约1个小时来实现,而且它确保我节省了10个小时的麻烦。
考虑使用“SwiftMailer”框架从您的应用程序发送邮件。它是一个强大可靠的PHP邮件框架,它是免费的。起初我很难整合2个框架,然后我找到了这个博客,我在几分钟内完成了任务。
http://grafikkaos.co.uk/blog/article/101-using-swift-mailer-with-codeigniter