I think everyones given correct answers here but they aren't the nicest or even radest examples.
Let's spice up your initial code a bit. Like everyone has said, you'll need to write a bit of PHP on the server side to handle what's being passed by jQuery but right now everyone's given pretty "static" versions of this methodology.
An okay example JS:
function new_user(auth_type, tr_id, user_name, email){
$.post("bookmark.php",{Function:doSomething, AuthType:auth_type, TR_Id:tr_id, UserName:user_name, UserEmail:email});
}
You can see I added a variable for the function to call "Function:doSomething". doSomething()
will be the function executed server side.
This is a good way to call your functions dynamically but your variables are still sitting all together with that function var. This means you'll eventually have to write code to grab each one separately in order to pass them into that function (server side). Not very elegant.. soo we'll write an even better example...
New, and far more awesome, JS:
function new_user(auth_type, tr_id, user_name, email){
$.post("bookmark.php", {Function: doSomething, Data: [{ Authtype, TR_ID:tr_id, UserName:user_name, Email:email}] });
}
In this new example you can see I've wrapped your secondary variables/data in a Data variable. It's easier this way to pass multiple parameters to your PHP file / function.
Now: Wicked, awesome, scalable bookmark.php
:
<?php
if(isset($_POST['Function'])){
call_user_func($_POST['Function'], $_POST['Data']);
}
doSomething($data=array()){
print_r($data);
}
?>
As you can see, all you have to add in your bookmark.php
file is a check to make sure that the Function variable has been passed and then use PHP's call_user_func()
which will call a function for you and pass in the variable Data (which is the array of values you send in your $.post()
).
You'll notice I only set one parameter for the function doSomething()
which expects an array. This means you could send any parameters into that function.
Any function can be called this way and you'll find wrapping all your parameters into an array helps keep everything dynamic and scalable.
If you have any questions about this code feel free to ask.