Server-Side Functions with AJAX
Hey everyone. I’m trying to figure out the most efficient way to call a PHP function with jQuery. I thought you guys might be able to offer your own methods.
At the moment, I’m doing…
$.post(
'pageToLoad.php',
{ func: 'doSomething' },
function(results) {
console.log(results)
});
The $.Post Method
$.post accepts a total of four parameters:
- Url: Page to load
- Data: Key/value pairs to pass.
- Callback: Function to run after operation has completed.
- Type: Type of returned data
So let’s assume that ‘pageToLoad.php’ stored a bunch of functions, and we only wanted to call one specific method. Well, we could – as many have suggested on the forums – simply copy and paste that function into its own file, but this hardly seems efficient. What to do?
My Method
Now I could very easily be making this more difficult than it is. That’s why I’m curious as to how you handle these types of situations. I’ve been passing the function name as a key/value when we call the PHP page with jQuery.
{ func: 'doSomething' }
Then, from my PHP file (in this case, “pageToLoad.php”), I use the $_POST super-global to grab that value and execute it; like so:
// pageToLoad.php
// This will grab the value that was passed through - 'doSomething'.
// We then execute the function by appending the '()'.
$_POST['func']();
function someFunction() {
bla bla
}
function doSomething() {
echo 'hello world';
}
It Works!
This method works perfectly for my needs (with the obvious addition of some security checks). I’m only curious… how do you do it?
Comments
Chuckytuh said...
awhh
there was a big reply list and discussion going on this post about the security issues when making calls to php with AJAX! Well I’m new to AJAX world and actually I’m quite curious about what type of security mashups exists and how to achieve them!
Thanks for all the contribute you gave for the community and for me!
posted on September 23, 2009
Jeffrey Way said...
I know. I haven’t had the chance to import that comments table. I’ll be sure to revise this article with a more real-world example.
posted on September 23, 2009
David Ferguson said...
Before I finished reading the description, I was personally thinking passing the function name as a parameter, then running that value through a switch or if/else condition to determine how to move on. Eliminates the possibility of an unlink() or something potentially being called because of the passed parameter. You would be limiting the list of available functions.
Was just my first thought though..
posted on September 24, 2009
Moksha said...
Its really simple with php any idea how to do it in ASP.net
thanks
posted on September 24, 2009
ARIF said...
<b<Thanks, Jeffrey
when we use ajax call in php which return the ajax xml request like:
echo $prod_code.”|”.$prod_des.”|”.$qty_code.”|”.$num_packs;
then we split that and show the data in several field Like
if (xmlHttp.readyState==4 || xmlHttp.readyState==”complete”)
{
var ar=xmlHttp.responseText.split(”|”);
document.getElementById(”txtprice”).value=ar[2] ;
document.getElementById(”txtDiscountPrice”).value=ar[3] ;
}
<Br.
So how can it will be done in jquery ajax call. In your above technical.
posted on September 25, 2009
Programmieraffe said...
Hey,
maybe this concept is interesting for you:
http://blog.extjs.eu/know-how/mastering-ext-direct-part-1/
posted on September 29, 2009
Martin Førre said...
You could use a prefix on the function names?
{ func: ‘doSomething’ }
# // pageToLoad.php
$func = $_POST['func']();
if(function_exists(’pre_’ . $func))
{
$func = ‘pre_’ . $func;
$func();
}
function pre_doSomething()
{
echo ‘hello world’;
}
posted on October 13, 2009
Martin Førre said...
correction: $func = $_POST['func'](); should be $func = $_POST['func'];
posted on October 13, 2009
kreativo said...
Thanks for the tutorial Jeff
.
I was wondering if it could be possible for you to make an ajax tutorial about removing files from folders.
posted on December 10, 2009