2012-06-12 29 views
1

如何在不刷新的情況下創建jQuery + ajax表單? 這是我的控制器和意見:Kohana - 如何創建沒有刷新的窗體?

http://pastebin.com/GL5xVXFZ

在 「清除」 PHP創建這樣的事情:在add.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; 
    }); 
}); 

是INSERT到數據庫。

回答

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

    } 
?> 

這是一個例子,我已經沒有測試完成,但這個肯定應該足夠讓你在

工作