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');
Award.php (located in the /controller/ folder)
<?php foreach (glob("../model/community/*.php") as $filename) { include $filename; } $award = new Award();
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.