Skip to content Skip to sidebar Skip to footer

Make Jquery Ajax Call To Specific Php Functions

I am a rookie PHP developer. I have created a PHP web project with an HTML page that contains an 'Add' button. The name of the page is awards.html. The awards.html file contains it

Solution 1:

Okay I found the solution to the problem myself.

I sent a GET-type AJAX call with the data passed in the form of String parameters and added a success property to get a confirmation whether my code has worked or not. I also changed my Award.php code to catch the passed parameter successfully.

So the source code of my files is:

awards.js

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

functiononSave() {
    $("#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();
} elseif ($functionName == "save") {
    save();
}

functionadd()
{
    $award = new Award();
    $method = $award->addFunction();
    echo json_encode($method);
}

functionsave()
{
    $award = new Award();
    $method = $award->saveFunction();
    echo json_encode($method);
}

Solution 2:

You would be better off using an MVC framework in which you can have controller functions for add and save 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:

<?phpforeach (glob("../model/community/*.php") as$filename) {
    include$filename;
}

functionadd(){
    $award = new Award(); //Instantiating Award in /model/ folder$method = $award->addFunction();
    echo json_encode($method);
}

functionsave(){
    $award = new Award(); //Instantiating Award in /model/ folder$method = $award->saveFunction();
    echo json_encode($method);
}

if($_GET["action"] == "save")
  save();
elseif($_GET["action"] == "add")
  add();

Your js code will look like:

functiononAdd() {
    $("#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);
    });
}

functiononSave() {
    $("#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);
    });
}

Solution 3:

There is a method that I have discovered. Please read my explanation completely to continue. In your ajax code (I think you use jQuery), include another info 'perform', with value = php function name.

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

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

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

Then, when you call using ajax, you can get a particular function executed. Example Usage : Javascript :

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

PHP file example.php:

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

When you do this, automatically, function register() will get executed.

Post a Comment for "Make Jquery Ajax Call To Specific Php Functions"