0

I had a question; How would one call multiple PHP functions and output them onto the page?

Now i have found a way, could anyway let me know how i could improve my answer. It works perfectly, I just want to see what could be a better way.

AJAX CALL;

$.ajax({ 
    url: 'functioncalls.php', //URL TO PHP FUNCTION CONTROL
    data: {call: 'Login', parameters: {Username, Password}}, //CALL the function name with an array of parameters
    type: 'post'
}).done( function( Output ){
    try{
     Output  = JSON.parse(Output); // see if out put is json as that is what we will pass back to the ajax call
    } catch (e){
        alert('Error calling function.');
    }
});

PHP "functioncalls.php" page

if(isset($_POST['call']) && !empty($_POST['call'])){ //check if function is pasted through ajax
    print call_user_func_array($_POST['call'], $_POST['parameters']);//dynamically get function and parameters that we passed in an array
}

PHP FUNCTION - make sure your function is either on the page or included

function Login($Username, $Password){
    // function control
    return json_encode($return);// return your json encoded response in value or array as needed
}

And that's that, Nothing else needed you can call any function and use it in your done ajax promise.

Note: Your parameters have to be passed as an array.

Thank you

4
  • i think your problem is that post doesn't work with multidimensional inputs. just simple key value pairs. I had this problem too. Commented Mar 16, 2017 at 8:17
  • 2
    You are reinventing RPC/SOAP. Why not thinking about REST for decoupling frontend and backend ? Commented Mar 16, 2017 at 8:17
  • @mtizziani what would be the reasoning behind multidimensional inputs, you could pass them through to php and refactor them there? Commented Mar 16, 2017 at 8:31
  • @n00dl3 This just seems easier, I don't completely understand nor have a iused RCP/SOAP before, but I will look into it. thank you Commented Mar 16, 2017 at 8:34

1 Answer 1

1

change your ajax request like this

$.ajax({ 
    url: 'functioncalls.php', //URL TO PHP FUNCTION CONTROL
    data: {call: 'Login', parameters: JSON.stringify([Username, Password])}, //CALL the function name with an array of parameters
    type: 'post'
}).done( function( Output ){
    try{
     Output  = JSON.parse(Output); // see if out put is json as that is what we will pass back to the ajax call
    } catch (e){
        alert('Error calling function.');
    }
});

in php you have to do something like this:

$params = json_decode($_POST['parameters']);
login($params[0], $params[1]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Sorry Im just trying to understand here, What would be the reasoning behind passing json stringified array through to the function, that would require further manipulation on the function side, while adding no extra security or more diverse options. If there are other reason to to encode the array in Json before sending to php please let me know.
i asked this question a few weeks ago too. here is the link -> stackoverflow.com/questions/41717877/… . the answer is, in every key value pair that you send via post the value has to be of type string. otherwise it can have negitive side effects. i think $_POST is defined as array of strings when filter_input function calls it

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.