Calling a specific function from PHP with Jquery & Ajax

jwknz

For me this is something new, so I am just researching this and trying to understand it. As you can see in the php script there are 2 functions and I am trying to call a specific one with jquery.

Now if I have one function then I can do it, but when I have 2 or more I am starting to get stuck. I suppose I could do this when I have 2 functions, but as soon as more variables are in play or more functions do I just make massive if statements in my php?

The problem is that when I attach a database to it, I would need to consider all inputs that can happen. How do I specify a specific php function when using jquery & ajax?

//function.php
<?php
    function firstFunction($name)
    {
        echo "Hello - this is the first function";
    }

    function secondFunction($name)
    {
        echo "Now I am calling the second function";
    }

?>

<?php
    $var = $_POST['name'];

    if(isset($var))
    {       
        $getData = firstFunction($var);
    }
    else if(isset($var))
    {
        $getData = secondFunction($var);
    }
    else
    {
        echo "No Result";
    }

?>

//index.html
<div id="calling">This text is going to change></div>

<script>

     $(document).ready(function() {
        $('#calling').load(function() {
        $.ajax({
            cache: false,
            type: "POST",
            url: "function.php",
            data: 'name=myname'
            success: function(msg)
            {
                $('#calling').html((msg));
            }

            }); // Ajax Call
        }); //event handler
    }); //document.ready

</script>
Robbie Averill

You need to pass a parameter in, either via the data object or via a GET variable on the URL. Either:

url: "function.php?action=functionname"

or:

data: {
    name: 'myname',
    action: 'functionname'
}

Then in PHP, you can access that attribute and handle it:

if(isset($_POST['action']) && function_exists($_POST['action'])) {
    $action = $_POST['action'];
    $var = isset($_POST['name']) ? $_POST['name'] : null;
    $getData = $action($var);
    // do whatever with the result
}

Note: a better idea for security reasons would be to whitelist the available functions that can be called, e.g.:

switch($action) {
    case 'functionOne':
    case 'functionTwo':
    case 'thirdOKFunction':
        break;
    default:
        die('Access denied for this function!');
}

Implementation example:

// PHP:
function foo($arg1) {
    return $arg1 . '123';
}

// ...
echo $action($var);

// jQuery:
data: {
    name: 'bar',
    action: 'foo'
},
success: function(res) {
    console.log(res); // bar123
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Wordpress PHP function from AJAX

From Dev

Calling a specific Python version from system function in PHP

From Dev

Issue calling JS function from PHP echo with a specific variable

From Dev

Calling a function from ajax in WordPress

From Dev

Calling PHP function file_put_contents From Inside JQuery

From Dev

Sending some data from jQuery, calling function that is in PHP by setInterval

From Dev

JQuery calling a PHP function issue

From Dev

Error in calling PHP function in jquery

From Dev

PHP and JQuery Function Calling Repeated

From Dev

How to return specific data from php to jquery ajax?

From Dev

Calling a Function in PHP file, using AJAX, with an ID specific to a record in Database to update record

From Dev

Calling controller function from jquery

From Dev

Jquery ajax not calling server side function

From Dev

Ajax success and error function is not calling properly in jquery

From Dev

Ajax calling multiple php files in same function

From Dev

Calling a php function in a separate file using AJAX

From Dev

calling OOP PHP function via AJAX

From Dev

Removing a WordPress filter by calling a PHP function with Ajax

From Dev

Wordpress ajax is not calling function in functions.php

From Dev

Call php function with parameters from ajax (DataTable jQuery)

From Dev

jquery - calling function from inside function

From Dev

calling ajax function from knockout value undefined

From Dev

Calling Web Service from jQuery .ajax() issue

From Dev

Calling php function from inside another function

From Dev

calling servlet from php page using ajax

From Dev

Datatable ajax calling from php page

From Dev

Calling a php function unsing jquery goes wrong

From Dev

Calling ajax function continuously at a specific interval until a condition is met?

From Dev

Calling a function inside a jQuery plugin from outside

Related Related

  1. 1

    Calling Wordpress PHP function from AJAX

  2. 2

    Calling a specific Python version from system function in PHP

  3. 3

    Issue calling JS function from PHP echo with a specific variable

  4. 4

    Calling a function from ajax in WordPress

  5. 5

    Calling PHP function file_put_contents From Inside JQuery

  6. 6

    Sending some data from jQuery, calling function that is in PHP by setInterval

  7. 7

    JQuery calling a PHP function issue

  8. 8

    Error in calling PHP function in jquery

  9. 9

    PHP and JQuery Function Calling Repeated

  10. 10

    How to return specific data from php to jquery ajax?

  11. 11

    Calling a Function in PHP file, using AJAX, with an ID specific to a record in Database to update record

  12. 12

    Calling controller function from jquery

  13. 13

    Jquery ajax not calling server side function

  14. 14

    Ajax success and error function is not calling properly in jquery

  15. 15

    Ajax calling multiple php files in same function

  16. 16

    Calling a php function in a separate file using AJAX

  17. 17

    calling OOP PHP function via AJAX

  18. 18

    Removing a WordPress filter by calling a PHP function with Ajax

  19. 19

    Wordpress ajax is not calling function in functions.php

  20. 20

    Call php function with parameters from ajax (DataTable jQuery)

  21. 21

    jquery - calling function from inside function

  22. 22

    calling ajax function from knockout value undefined

  23. 23

    Calling Web Service from jQuery .ajax() issue

  24. 24

    Calling php function from inside another function

  25. 25

    calling servlet from php page using ajax

  26. 26

    Datatable ajax calling from php page

  27. 27

    Calling a php function unsing jquery goes wrong

  28. 28

    Calling ajax function continuously at a specific interval until a condition is met?

  29. 29

    Calling a function inside a jQuery plugin from outside

HotTag

Archive