0

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!

5
  • 1
    inside myData.php, include/use the file where your class is defined and call ClassName::getproduct($p) Commented Nov 22, 2016 at 6:59
  • 1
    check this answer stackoverflow.com/questions/17489109/… Commented Nov 22, 2016 at 7:00
  • @user3099298 Im really stupid i didnt get it. Could you give me an more simple example?. Cheers. Commented Nov 22, 2016 at 7:09
  • @Ima The posted PHP/Mysql function is already inside myData.php (Who has 2 more functions), so, is like i'm calling it directly, is this wrong? Commented Nov 22, 2016 at 7:12
  • 1
    Ypu cannot call directly from javascript, you have to call a url with javascript (via Ajax) and that url should be handled in a php file. That php file can invoke any php function you want. Read for more info on ajax: w3schools.com/xml/ajax_intro.asp Commented Nov 22, 2016 at 7:45

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Now I'm Getting an Parse error: syntax error, unexpected 'class' (T_CLASS), expecting function (T_FUNCTION) . I leave you a pastebin to see how my myData.php looks pastebin.com/CExBQdsd
@fecapeluda Create two files in separate directory(not your actual project folder) and run above code, then you can understand how it is working. Then apply the concept to your actual project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.