Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a specific function from PHP with Jquery & Ajax

Tags:

jquery

ajax

php

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>
like image 295
jwknz Avatar asked Sep 05 '14 01:09

jwknz


People also ask

How to call specific function in php?

There are two methods for doing this. One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func() Function but in both method variable name is to be used. call_user_func( $var ); call_user_func( $var1 , "fun_function" );

Can jquery call php function?

Yes, this is definitely possible. You'll need to have the php function in a separate php file.

How to call specific php function in ajax?

$. ajax({ url: "functions. php", data: "function=time", // or function=date if you want date ... }); Then in your PHP code, a simple if-statement will check which one to output.

Can I call php function from Javascript?

You can call PHP function through Javascript by passing the value, you need as output of PHP function as a string in Javascript.

How to call a PHP function from JavaScript?

To summarise, you can use AJAX when you want to call a PHP function from JavaScript or run PHP code on some data generated inside browsers. You can use echo in PHP to output JavaScript code which will run later in the client's browser. If you have any questions about the article, please let me know in the comments.

How to call a PHP file from a jQuery file?

If you are using jQuery on your website, it becomes incredibly easy to call any PHP file with code that you want to run. You can pass one or two parameters to the ajax () function.

How to call a PHP function on data generated inside a browser?

We can use AJAX to call a PHP function on data generated inside a browser. AJAX is used by a lot of websites to update parts of webpages without a full page reload. It can significantly improve the user experience when done properly. Keep in mind that the PHP code will still run on the server itself.

How to run PHP code on a server using jQuery?

Keep in mind that the PHP code will still run on the server itself. We will just provide it with data from within our script. If you are using jQuery on your website, it becomes incredibly easy to call any PHP file with code that you want to run. You can pass one or two parameters to the ajax () function.


1 Answers

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
}
like image 60
scrowler Avatar answered Sep 23 '22 19:09

scrowler