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)); } }); </script>
source share