【问题标题】:Kohana - how to create form without refresh?Kohana - 如何在不刷新的情况下创建表单?
【发布时间】:2012-06-12 09:38:27
【问题描述】:

如何在不刷新的情况下创建 jQuery + ajax 表单? 这是我的控制器和视图:

http://pastebin.com/GL5xVXFZ

在“清除”PHP 中,我创建了这样的内容:

$(document).ready(function(){
    $("form#submit").submit(function() {

    var note = $('#note').attr('value');

        $.ajax({
            type: "POST",
            url: "add.php",
            data: "note="+ note,
            success: function(){
                $('form#submit').hide(function(){$('div.success').fadeIn();});

            }
        });
    return false;
    });
});

在 add.php 文件中是 INSERT 到数据库。

【问题讨论】:

    标签: php jquery ajax kohana


    【解决方案1】:

    有更复杂的方法来执行此操作,例如在您的操作中检测 ajax 请求,然后如果检测到,则打印出 javascript 响应。你会这样做的方式是

    JAVASCRIPT

    function postForm(note){
         $.ajax({
          url  : '/controller/action',
          type : 'POST',
          data : 'note='+note,
          success : function(jsn){
            var json = $.parseJSON(jsn);
            if(json.status == 200)
              alert('Completed Successfully');
            else
              alert('Not Completed Successfully');
          },
          error : function(xhr){
            //Debugging
            console.log(xhr);
          }
    
    
         });
       }
    

    PHP

      <?php  
      Class Controller_ControllerName extends Controller_Template{
          public $template = 'template';
    
          public function action_index(){}
    
          public function action_form(){
            $this->auto_render = false; // <-EDITED
            $array = array();
    
            //PROCESSING FORM CODE HERE
    
            if($success){
              $array['status'] = 200;
            }else{
              $array['status'] = 500; 
            }
    
            print json_encode($array);
          }
    
        }
    ?>
    

    这是一个我在没有测试的情况下完成的例子,但这肯定足以让你继续工作

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 2019-12-21
      • 1970-01-01
      • 2019-11-25
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多