【问题标题】:Ajax Form submitAjax 表单提交
【发布时间】:2011-05-09 04:22:45
【问题描述】:

我是阿贾克斯的新手。我想通过 Ajax 提交表单并将发送的数据保存到数据库中。

类似于 Facebook 状态更新 - 文本区域被禁用然后提交。一旦它保存在数据库中,它就会返回并更新顶部的状态消息。再次启用文本区域。

这是我的表格

  <?php echo $form->create('StatusMessage', array('type' => 'post', 'url' => '/account/updateStatus', 'id' => 'updateStatus')); ?> 
  <?php echo $this->Form->input('id', array('value' => $user['User']['id'],'type' => 'hidden')); ?> 
  <?php echo $this->Form->textarea('message', array('value' => 'What have you been eating ?')); ?>

已编辑:根据 0 RioTera 的建议修改

Cakephp 动作

function updateStatus() {

    $this->autoRender = false;
    if($this->RequestHandler->isAjax()) {
        $this->layout = 'ajax'; //THIS LINE NEWLY ADDED

        $this->data['StatusMessage']['pid'] = 0;
        $this->data['StatusMessage']['commenters_item_id'] = $this->data['StatusMessage']['item_id'] = $this->User->Item->itemId('1', $this->data['StatusMessage']['id']);
        unset($this->data['StatusMessage']['id']);
        //debug($this->data);

        if($this->User->Item->StatusMessage->save($this->data)) {
        return true;
        } else {
            echo 'not saved';
        }
    } else {
        echo 'no';
    }

}

Javascript 代码

$(document).ready(function () {
    var options = {
        target: '#output2',
        // target element(s) to be updated with server response 
        beforeSubmit: showRequest,
        // pre-submit callback 
        success: showResponse, // post-submit callback 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };

    $('#updateStatus').submit(function () {
        // make your ajax call
        $(this).ajaxSubmit(options);
        return false; // prevent a new request
    });

    function showRequest(formData, jqForm, options) {
        $('#StatusMessageMessage').attr('disabled', true);
    }

    function showResponse(responseText, statusText, xhr, $form) {
        $('#StatusMessageMessage').attr('disabled', false);
        alert('shdsd');
    }
});

【问题讨论】:

    标签: ajax cakephp jquery cakephp-1.3


    【解决方案1】:

    这是我在 cakephp 3.x 中用于提交表单的函数,它使用甜蜜警报,但可以更改为普通警报。这是非常可变的,只需在您的控制器中放置一个动作来捕获表单提交。此外,位置重新加载将重新加载数据以向用户提供即时反馈。可以取出来的。

    $('#myForm').submit(function(e) {
        // Catch form submit
        e.preventDefault();
    
        $form = $(this);
        // console.log($form);
        // Get form data
        $form_data = $form.serialize();
        $form_action = $form.attr('action') + '.json';
        // Do ajax post to cake add function instead
        $.ajax({
            type   : "PUT",
            url    : $form_action,
            data   : $form_data,
            success: function(data) {
                swal({
                    title: "Updated!", 
                    text: "Your entity was updated successfully", 
                    type: "success"
                },
                function(){
                        location.reload(true);
                });
            }
        });
    });
    

    这就是控制器中一个简单动作的样子;

    public function updateEntity($id = null){
        $entity = $this->EntityName->get($id);
        $entity = json_encode($this->request->data);
        $this->EntityName->save($entity);
    }
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        使用 jquery 和 http://jquery.malsup.com/form/ 插件:

        $(document).ready(function() { 
            var options = { 
                target:        '#message',   // target element(s) to be updated with server response 
                beforeSubmit:  showRequest,  // pre-submit callback 
                success:       showResponse  // post-submit callback 
            };
            $('#updateStatus').ajaxForm(options); 
        });
        
        function showRequest(formData, jqForm, options) {
            $('#StatusMessageMessage').attr('disabled', true);
        }
        
        function showResponse(responseText, statusText, xhr, $form)  { 
            $('#StatusMessageMessage').attr('disabled', false);
        }
        

        没试过,希望对你有帮助

        【讨论】:

        • 我有类似的代码。但 showRequest 没有采取任何措施。为什么我们需要 showRequest(formData, jqForm, options) ?
        • 和禁用工作。但在提交后启用不。如何从我提交的操作中取回一些数据。就像我想显示用户的姓名和照片以及消息和时间一样。在墙上
        • 您需要 showRequest 在单击提交按钮时禁用文本区域。现在您需要从控制器帐户的操作 update_status 返回一些数据。这种情况下的动作不必渲染整个页面,只渲染您需要的数据 (html, json) ($this->autoRender = false)。花一些时间来解释所有的过程。你现在必须阅读 cakephp 手册。
        • 编辑了代码..我已经完成了那部分。你是说直到一些数据没有返回成功回调方法不会被调用?
        • 是的,只有在收到有效响应时才会触发成功事件
        猜你喜欢
        • 2011-06-17
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-03
        相关资源
        最近更新 更多