php - cakephp 2.3 ajax表单提交

标签 php ajax jquery cakephp cakephp-2.0

<分区>

我正在尝试创建一个简单的表单来使用 ajax 将数据提交到数据库。这是我的观点

  <?php echo $this->Html->script('jquery', FALSE); ?> 
<?php echo $this->Html->script('validation', FALSE); ?>
  <div id="success"></div>
  <h2>Contact Us</h2>

  <?php
   echo $this->Form->create();
   echo $this->Form->input('name', array('id'=>'name'));
   echo $this->Form->input('email', array('id'=>'email'));
   echo $this->Form->input('message', array('id'=>'message'));
   echo $this->Js->submit('Send', array(
'before'=>$this->Js->get('#sending')->effect('fadeIn'),
'success'=>$this->Js->get('#sending')->effect('fadeOut'),
'update'=>'#success'
 ));
 echo $this->Form->end();
  ?>
   <div id="sending" style="display: none; background-color: lightgreen;">Sending...</div>

Controller

    class MessagesController extends AppController{

public $helpers = array('Js');
public $components = array('RequestHandler');

public function index(){

  if(!empty($this->data)){
      if($this->Message->save($this->data)){
          if($this->request->isAjax()){
             $this->render('success','ajax');
          }else{


         $this->Session->setFlash('Message sent');
         $this->redirect(array('action'=>'index'));
      }
      }
  }
 }

 }

它不是通过 ajax 发送表单。实际上我是 cakephp 的新手,这是我第一次在 cakephp 中使用 ajax。我还想知道我可以在 cake php 中使用 codeigniter 或简单的 php 类似的表单提交语法。例如像这样

  <script>

 $('#btn').click(function(event) {
       form = $("#form").serialize();

     $.ajax({
       type: "POST",
       url: "<?php  echo site_url('categoryController/addCategory'); ?>",
       data: form,

       success: function(data){
           $('.modal').modal('hide');
           $(".success").fadeIn(500).delay(2000).fadeOut(500);
           $("#form")[0].reset();
           //Unterminated String constant fixed
       }

     });
     event.preventDefault();
     return false;  //stop the actual form post !important!

  });

</script>

最佳答案

我得到了答案。我缺少 jquery.js 文件。

关于php - cakephp 2.3 ajax表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17091907/

相关文章:

php - 即使未选择,Laravel 下拉列表也会获取所有选项

php - 使用表单发布(php)将文件上传到Azure存储,并返回链接

javascript - jquery 在 body 更换时播放声音

javascript - 将对象列表转换为 json 树结构

jquery - 使用 jQuery 选择内部文本

php - PHP 7.1 中的隐式无效返回?

php - 如何在Jquery中包含带参数的php函数

php - 域名注册 API

php - 将参数从 jquery 传递到 php

javascript - 如何解决跨域请求被阻塞?