I'm trying to submit a form via AJAX and prevent a redirect after the form is submitted. I don't understand why I'm having this issue since I set autoRender to false.
Controller Code
function add() {
$this->autoRender = false;
if (!empty($this->data)) {
$this->data['Comment']['user_id'] = $this->Auth->user('id');
$this->Comment->create();
if ($this->Comment->save($this->data)) {
}
}
}
JS event handler
$(".submit_comment").live("submit",commentSubmitHandler);
function commentSubmitHandler(event){
$.ajax({
type: "post",
url: $("#webroot").val() + "comments/add",
data: data,
dataType: "json",
success: function(data){
alert("win");
},
error: function(data){
alert("fail");
}
});
return false;
}
The form data is submitted and saved just fine, but why the heck is it leaving the page? Also, it seems to be doing it before finishing the js because the alerts never actually go off. Therefore there is a definite redirect straight from the controller action "add."