2011-09-27 22 views
0

對不起,這個主題可能是不正確的標題,但這是我想到的最好的。添加表單時PHP重新加載頁面(可能需要ajax)

所以,我正在建立一個網站的管理面板。

我有一個頁面,並在頁面的某個部分,我想刷新它並加載另一個表單。

比方說,添加一個時間表,並在頁面上的某個地方,我希望點擊鏈接後立即顯示此表單。

當用戶保存它時,我希望那個表單消失,而不是有一個列表顯示所有的時間表。

enter image description here

我不想使用框架 - 我不是幀的支持者。該面板使用PHP構建。

也許這可能是用Ajax實現的?如果是 - >如何?任何鏈接到良好的例子或教程。

回答

1

是的,這將用ajax解決。

下面是當頁面應該刷新

$('#button').click(function() { 
    $.ajax({ 
     url: 'path/to/script.php', 
     type: 'post', 
     dataType: 'html', // depends on what you want to return, json, xml, html? 
         // we'll say html for this example 
     data: formData, // if you are passing data to your php script, needed with a post request 
     success: function(data, textStatus, jqXHR) { 
      console.log(data); // the console will tell use if we're returning data 
      $('#update-menu').html(data); // update the element with the returned data 
     }, 
     error: function(textStatus, errorThrown, jqXHR) { 
      console.log(errorThrown); // the console will tell us if there are any problems 
     } 
    }); //end ajax 

    return false; // prevent default button behavior 
}); // end click 

jQuery的阿賈克斯

http://api.jquery.com/jQuery.ajax/

腳本解釋的代碼示例。

1 - 用戶單擊按鈕。

2 - 點擊功能啓動一個XHR呼叫到服務器。

3 - url是一個php腳本,它將根據發佈的值處理我們發送的數據。

4 - 該類型是一個POST請求,它需要數據返回數據。

5 - 在這種情況下的dataType將是html。

6 - 我們發送給腳本的數據可能是分配給變量formData的表單元素的序列化。

7 - 如果XHR返回200,則在控制檯中登錄返回的數據,以便我們知道我們正在處理什麼。然後將數據作爲html放入選定元素(#update-menu)中。

8 - 如果出現錯誤,控制檯會爲我們記錄錯誤。

9 - 返回false以防止默認行爲。

10 - 全部完成。

相關問題