简体   繁体   中英

Ajax call PHP Mysql public static function

Im getting a hard time with this guys, How can i call this kind of php class? Here rest my problem remote: 'myData.php?jquery=%jquery' What's the correct call format?

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:

$(document).ready(function() {

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

            });

        })

Thanks!

Product.php file represents your class file. viewProduct.php is your view file. When you enter text on city input field, jQuery will pass that value to class file using GET method. If class file get a value from myRequest , then it will call static function and send output to the view file.

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

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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