2010-11-12 150 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,而无需离开当前页面。 所以你不能直接调用一个类的功能......但你可以通过允许构造函数根据你已经传递的参数调用这个函数来处理它