简体   繁体   English

Ajax调用PHP Mysql公共静态函数

[英]Ajax call PHP Mysql public static function

Im getting a hard time with this guys, How can i call this kind of php class? 我和这些家伙很难过,我怎么称呼这种php类? Here rest my problem remote: 'myData.php?jquery=%jquery' What's the correct call format? 这是我的远程问题的解决方法:'myData.php?jquery =%jquery'什么是正确的调用格式?

myData.php myData.php

public static function getproduct($p){
        $sql = "select * from products where code like '%$p%' or name like '%$p%' or id like '%$p%'";
        $query = Executor::doit($sql);
        return Model::many($query[0],new ProductData());
    }

From Js/Ajax like: 来自Js / Ajax,例如:

$(document).ready(function() {

            $('input.city').typeahead({
                name: 'city',
                remote: 'myData.php?jquery=%jquery'

            });

        })

Thanks! 谢谢!

Product.php file represents your class file. Product.php文件代表您的类文件。 viewProduct.php is your view file. viewProduct.php是您的视图文件。 When you enter text on city input field, jQuery will pass that value to class file using GET method. 当您在城市输入字段上输入文本时,jQuery将使用GET方法将该值传递给类文件。 If class file get a value from myRequest , then it will call static function and send output to the view file. 如果类文件从myRequest获取值,则它将调用静态函数并将输出发送到视图文件。

Product.php Product.php

class Product{
    public static function getproduct($p){
        // add your SQL queries
        return "This is return value ".$p;
    }
}

// call static function 
if(isset($_GET['myRequest'])) {
     echo Product::getproduct($_GET['myRequest']);
}

viewProduct.php viewProduct.php

<input type="text" name="city" id="city"/>

<span id="result"></span>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#city").keyup(function(){
        var txt = $(this).val();
        $.get("product.php", {myRequest: txt}, function(result){
            $("#result").html(result);
        });
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM