2013-01-24 120 views
0

我已經創建了一個php函數(在「category-page.php」中稱爲「category_page」),它將文件中的數據讀入關聯數組,然後生成一些html以顯示信息on the products.php(頁面調用「category_page」函數)更新php生成的內容而不刷新頁面

我的目標是允許用戶從下拉列表中選擇,以便在不刷新頁面的情況下對顯示的信息進行排序。

到目前爲止,我已設法使用document.formname.submit更改下拉列表,然後使用$_GET來選擇要排序的數組中的哪個鍵,但是,這會導致頁面重新加載。

我有PHP,JavaScript的/ jQuery的的知識,也做搜索的公平位/閱讀AJAX啓用更新不刷新/重載,但似乎無法把所有的拼在一起。

所以,在products.php,我有以下的javascript/jQuery的:

function sort_products() { 
queryString = "?sort_list="+$("#sort_list").val(); 
$.ajax({ 
    type: 'GET',         
    url: 'category-page.php',   
    data: 'sort_list='+queryString 
}) 
} 

$("#sort_list").on("change", function() { sort_products() }); 

,然後在類別-page.php文件:

if(isset($_GET['sort_list'])) { 
    $sort = $_GET['sort_list']; 
} 
else { 
    // set default sort order 
} 

我在Chrome的網絡面板驗證發送的請求是category-page.php?sort_list=price,但該頁面未更新。任何幫助,將不勝感激!

回答

0

更改這行代碼:

$("#sort_list").on("change", function(e) { sort_products(); e.preventDefault(); e.stopPropagation(); }); 
0

一旦查詢被髮送,你需要與返回什麼東西。

$.ajax({ 
    type: 'GET',         
    url: 'category-page.php',   
    data: 'sort_list='+queryString 
}) 
.done(function(data) { 
    // if your php code returns the html you can make something like this 
    // the var data will be the html code 
    $('#your-container').html(data) 
})