I'm trying to do a simple form mail with ajax. But, I can't grab the data from the form.
My form:
<?php echo $this->Form->create('Page', array('default' => false)); ?>
<?php echo $this->Form->input('texto', array('label' => FALSE, 'type' => 'textarea)); ?>
<?php echo $this->Form->submit('Enviar', array('id' => 'enviar'));
echo $this->Form->end();
My ajax:
$(document).ready(function() {
$('#enviar').click(function(){
$.ajax({
type: 'post',
complete: function(r){
$('div.teste').html('<h4> Enviado!</h4>');
}
})
})
});
The controller:
if($this->request->is('ajax')) {
debug($this->request->data);
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('gmail');
$Email->to('xxxx@gmail.com');
$Email->subject('Nova Mensagem - Site Althi');
$Email->send($mensagem);
}
}
And my controller send the email. The e-mail has send sucefull, but the data of this->request->data
is a blank array. I think the problem is the data passed from the ajax. Can help me please?
You have to set the data
value in the ajax function call.
$.ajax(
thecontrollerurl,
{data: $("#theformid").serialize()}
);
more info here: http://api.jquery.com/jQuery.ajax/
$data = $this->Js->get('#YourformORelementID')->serializeForm(array('isForm' => true, 'inline' => true));
And then
$this->Js->get('#enviar');
$this->Js->event('click',
$this->Js->request(
array('controller' => 'someController', 'action' => 'someAction'),
array('async' => true, 'data' => $data, 'update' => 'div.teste')
));