开发者

Include multiple functions in a PHP file and call them through jquery.post or jquery.get in a javascript file

开发者 https://www.devze.com 2023-01-04 04:16 出处:网络
I want to call various functions made in PHP from my javascript file. Normally using Jquery.post we call a PHP file and and pass various va开发者_开发百科lues as post.

I want to call various functions made in PHP from my javascript file. Normally using Jquery.post we call a PHP file and and pass various va开发者_开发百科lues as post.

 function new_user(auth_type, tr_id, user_name, email)
    {
    $.post("bookmark.php",{AuthType:auth_type, TR_Id:tr_id, UserName:user_name, UserEmail:email});
    }

If I want to create multiple functions in the PHP file and call them through jquery/ajax how do i do that?


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.


send along a variable in your post designating what function you want to call in your php file, then add a switch statement to the start of your php file and look at a designated variable say $_POST['function'], set the different conditions and have them call the corresponding function

<?php

switch($_POST['function']){
    case '1': do_this(); break;
    case '2': do_that(); break;
    case '3': do_the_other(); break;
    default: break;
}

function do_this(){
    echo 'do this called';
}
function do_that(){
    echo 'do that called';
}
function do_the_other(){
    echo 'do the other called';
}

?>


You can add other parameters to the ajax call. What i would do is add parameters for 'function'(string) and 'arguments'(array) so the php script on the other side knows what to do with the data being passed over.


Send an extra parameter in your Jquery post that defines what function you want to call in your PHP file. One possible example (easiest to implement):

For function 1

$.post("bookmark.php?f=1",{AuthType:auth_type, TR_Id:tr_id, UserName:user_name, UserEmail:email});

Note: Do some hardcore checking on your side to ensure someone didn't 'Fiddle' with your URL/Post variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消