Calling a specific function from PHP using Jquery & Ajax

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

Now, if I have one function, I can do it, but when I have 2 or more, I start to get stuck. I suppose I can do this when I have 2 functions, but as soon as more variables play the game 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 will need to consider all the source data that may occur. How to specify a specific php function when using jquery and 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> 
+6
source share
3 answers

You need to pass the parameter either through the data object or through the GET variable in the URL. Or:

 url: "function.php?action=functionname" 

or

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

Then in PHP you can access this attribute and process 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. For security reasons, a better idea would be to have white lists of available functions that can be called, for example:

 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 } 
+10
source

You are actually very close to what you want to achieve.

If you want to specify which function will be called in PHP, you can pass a variable to tell PHP. For example, you passed request=save to AJAX, you can write PHP as follows:

 $request = ''; switch(trim($_POST['request'])) { case 'save': $player_name = (isset($_POST['playername']) ? trim($_POST['player_name']) : 'No Name')); saveFunction($player_name); break; case 'load': loadFunction(); break; default: // unknown / missing request } 

EDIT: You can even pass along with other parameters

+3
source

This may not be exactly what you are looking for, but it may help some others find a very simple solution.

In jquery declare a variable and submit it

 var count_id = "count"; data: { count_id: count_id }, 

Then in your php check if this variable is set

 if(isset($_POST['count_id'])) { Your function here } 
0
source

Source: https://habr.com/ru/post/974860/


All Articles