Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call PHP function from jQuery?

Tags:

jquery

ajax

php

I have a PHP function on my site which takes a couple of seconds to complete. This holds the whole page up which I don't want.

Would it be possible with jquery to call this PHP function after the page has loaded and display the results in a div? Also to display an ajax loader image until the PHP function has completed?

I've been looking at jQuery.post but can't seem to get it to work.

Would someone be able to help?

Thank you

like image 378
Brigante Avatar asked Aug 23 '10 15:08

Brigante


People also ask

Can I call PHP function from JQuery?

Also, apart from doing this with the click of a button, a PHP function can be called using Ajax, JavaScript, and JQuery.

Can JavaScript call PHP function?

You can call PHP function through Javascript by passing the value, you need as output of PHP function as a string in Javascript.

Can ajax call a PHP function?

If I understand correctly, yes you can. Put all your functions in one php file and have the ajax pass as a parameter which one you want to call. Then with a switch or if structure, execute the one you want. Save this answer.

How do I call a PHP function from another file?

To call a function from another file in PHP, you need to import the file where the function is defined before calling it. You can import a PHP file by using the require statement. To call the greetings() function from another file, you need to import the library.


1 Answers

AJAX does the magic:

$(document).ready(function(

    $.ajax({ url: 'script.php?argument=value&foo=bar' });

));
like image 70
Otar Avatar answered Sep 20 '22 15:09

Otar