只是想在 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->input->post('name'); (虽然我有它的工作!)
..那么我做错了什么?
注意 - 我把 html_entity_decode 放在那里,因为我注意到电子邮件地址被发布为“address%40gmail.com”......所以这似乎是个好主意。有必要吗?