【问题标题】:Grab a form data with ajax使用 ajax 获取表单数据
【发布时间】:2013-05-15 01:06:58
【问题描述】:

我正在尝试使用 ajax 做一个简单的表单邮件。 但是,我无法从表单中获取数据。

我的表格:

<?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();

我的 ajax:

$(document).ready(function() {

$('#enviar').click(function(){
$.ajax({
  type: 'post',
  complete: function(r){
    $('div.teste').html('<h4> Enviado!</h4>');
  }
  })
 })
});

控制器:

    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);

            }

}

然后我的控制器发送电子邮件。 电子邮件已发送成功,但this-&gt;request-&gt;data 的数据是一个空白数组。 我认为问题是从 ajax 传递的数据。 可以帮帮我吗?

【问题讨论】:

  • 我在你的脚本中没有看到this-&gt;request-&gt;data。
  • 用控制器编辑
  • 你的表单中有$mensagem吗?

标签: javascript php jquery ajax cakephp


【解决方案1】:

您必须在 ajax 函数调用中设置 data 值。

$.ajax(
    thecontrollerurl,
    {data: $("#theformid").serialize()}
);

更多信息在这里:http://api.jquery.com/jQuery.ajax/

【讨论】:

  • 我不使用cakephp,但我相信它应该类似于您处理post/get请求的普通控制器。尝试调试请求数据。
【解决方案2】:
$data = $this->Js->get('#YourformORelementID')->serializeForm(array('isForm' => true, 'inline' => true));

然后

$this->Js->get('#enviar');
$this->Js->event('click', 
$this->Js->request(
    array('controller' => 'someController', 'action' => 'someAction'),
    array('async' => true, 'data' => $data, 'update' => 'div.teste')
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多