2010-11-12 157 views
0

我有一個下拉列表,我想通過jquery填充。我面臨的問題是我想直接從jquery調用類函數。例如 我的class_function PHP類包含get_locations函數。如何在不引入第三頁的情況下使用jquery .post方法調用get_locations函數?通過jquery調用php類的功能

回答

4

我通常這樣做的方式是在if PHP頁面頂部檢查特殊模式。

jQuery的

$.get(
    'page.php?mode=ajaxGetLocations&someVar=' + $('#someVar').val(), 
    function(data) { $('#myDropDown').html(data); } 
); 

PHP - 附近的代碼

<?php 
if (isset($_REQUEST['ajaxGetLocations']) 
{ 
    $someVar = $_REQUEST['someVar']; 
    // Get your data here 

    // Output your select box here 
    ?> 
    <select> 
    ... 
    </select> 
    <?php 
    exit; // You don't want to keep processing the page 
} 
?> 
頂部
0

你不能,因爲jQuery和PHP運行在完全不同的環境 - 服務器上的PHP,客戶端上的jQuery。 jQuery在PHP完成後運行。

您想查看jQuery's Ajax functions以瞭解如何從Javascript調用PHP腳本。

1

升級與

if (isset($_GET['get_locations'])) { 
    echo $this->get_locations(); 
    return; 
} 

你的PHP頁面,並從jQuery的與

$.ajax({ 
//.. 
data: "get_locations=1", 
//... 
}); 
0

稱之爲阿賈克斯用來使來自瀏覽器的HTTP請求,使用JavaScript,而無需離開當前頁面。 所以你不能直接調用一個類的功能......但你可以通過允許構造函數根據你已經傳遞的參數調用這個函數來處理它