Make a jQuery AJAX call for certain PHP functions

I am a beginner PHP developer.

I created a PHP web project with an HTML page containing an Add button. The name of the page is award.html. The awards.html file contains its own JavaScript partner file, awards.js. The code is executed in this js file when you click the "Add" button. This code sends an AJAX call to the PHP class located in the /controller/ folder in the project named Award.php. This PHP file contains the code for executing a function called addFunction () in another Award.php file located in the /model/ folder in the project, which returns a JSON array and is displayed on the awards.html page.

The source code of my files is set as follows:

Awards.html

 <div class = "divbottom"> <div id="divAddAward"> <button class="btn" onclick="onAdd();">Add</button> </div> </div> 

awards.js

 function onAdd() { $("#divAddAward").load('controller/Award.php'); //The full path of the Award.php file in the web root $.post( 'classes/Award.php' ).success(function(resp) { json = $.parseJSON(resp); }); } 

Award.php (located in the /controller/ folder)

 <?php foreach (glob("../model/community/*.php") as $filename) { include $filename; } $award = new Award(); //Instantiating Award in /model/ folder $method = $award->addFunction(); echo json_encode($method); 

Award.php (located in the /model/ folder)

 <?php class Award { public function addFunction() { $array = array( 'status' => '1' ); return $array; } } 

My code works fine and error free. Now I want to add another button to award.html, Save, which, when pressed, calls the onSave () function in the JS file. Thus, the source code of my files will change to the following:

awards.html

 <div class = "divbottom"> <div id="divAddAward"> <button class="btn" onclick="onAdd();">Add</button> <button class="btn" onclick="onSave();">Save</button> </div> </div> 

awards.js

 function onAdd() { $("#divAddAward").load('controller/Award.php'); //The full path of the Award.php file in the web root $.post( 'classes/Award.php' ).success(function(resp) { json = $.parseJSON(resp); }); } function onSave() { $("#divAddAward").load('controller/Award.php'); //The full path of the Award.php file in the web root $.post( 'classes/Award.php' ).success(function(resp) { json = $.parseJSON(resp); }); } 

Now the problem is that since I want to name the same Award.php file from JS, how do I change my controller's Control.Press file? In my opinion, there should be two different functions that contain code for creating an instance of the Award.php class and calling another function; something like the following:

Award.php

 <?php foreach (glob("../model/community/*.php") as $filename) { include $filename; } function add(){ $award = new Award(); //Instantiating Award in /model/ folder $method = $award->addFunction(); echo json_encode($method); } function save(){ $award = new Award(); //Instantiating Award in /model/ folder $method = $award->saveFunction(); echo json_encode($method); } 

Award.php

 class Award { public function addFunction() { $array = array( 'status' => '1' ); return $array; } public function saveFunction() { $array = array( 'status' => '2' ); return $array; } } 

But how can I name these specific PHP functions from my JS file? If the code I assumed above is correct, then what should be my JS code? Can someone please advise me about this? Replies will be appreciated as soon as possible. Thank you in advance.

+8
javascript jquery html ajax php
source share
3 answers

Well, I found a solution to the problem myself.

I sent an AJAX call of type GET with the data passed as String parameters, and added the success property to get confirmation whether my code worked or not. I also modified my Award.php code to successfully catch the passed parameter.

So, the source code of my files:

awards.js

 function onAdd() { $("#display").load(filePath); $.ajax({ type: "GET", url: 'controller/Action.php', data: "functionName=add", success: function(response) { alert(response); } }); } function onSave() { $("#display").load(filePath); $.ajax({ type: "GET", url: 'controller/Action.php', data: "functionName=save", success: function(response) { alert(response); } }); } 

Award.php

 $functionName = filter_input(INPUT_GET, 'functionName'); if ($functionName == "add") { add(); } else if ($functionName == "save") { save(); } function add() { $award = new Award(); $method = $award->addFunction(); echo json_encode($method); } function save() { $award = new Award(); $method = $award->saveFunction(); echo json_encode($method); } 
+8
source share

You will be better off using the MVC environment in which you can have controller functions to add and maintain functionality. You can achieve the required behavior with your current implementation by sending a query string parameter to tell your php script which function to call:

 <?php foreach (glob("../model/community/*.php") as $filename) { include $filename; } function add(){ $award = new Award(); //Instantiating Award in /model/ folder $method = $award->addFunction(); echo json_encode($method); } function save(){ $award = new Award(); //Instantiating Award in /model/ folder $method = $award->saveFunction(); echo json_encode($method); } if($_GET["action"] == "save") save(); else if($_GET["action"] == "add") add(); 

Your js code will look like this:

 function onAdd() { $("#divAddAward").load('controller/Award.php'); //The full path of the Award.php file in the web root $.post( 'classes/Award.php?action=add' ).success(function(resp) { json = $.parseJSON(resp); }); } function onSave() { $("#divAddAward").load('controller/Award.php'); //The full path of the Award.php file in the web root $.post( 'classes/Award.php?action=save' ).success(function(resp) { json = $.parseJSON(resp); }); } 
+4
source share

There is a method that I discovered. Please read my explanation in full to continue. In your ajax code (I think you are using jQuery), add other "execute" information, with function name = php.

 $.post("something.php", { something1: something1value, something2: something2value, perform: "php_function_name" }); 

Then in your PHP file add this piece of code at the beginning:

 <?php if (isset($_POST["perform"])) $_POST["perform"](); ?> 

Then, when you call using ajax, you can get a specific function. Usage example:

Javascript:

 $.post("example.php", { name: "anonymous", email: "x@gmail.com", perform: "register" }, function(data, status) { alert(data); }); 

PHP file example.php:

 <?php if (isset($_POST["perform"])) $_POST["perform"](); function register() { echo "Hai " . $_POST["name"] . " ! Your email id is " . $_POST["email"] . "."; } ?> 


When you do this, the register () function will automatically execute.

+1
source share

All Articles