Make jQuery AJAX Call to Specific PHP Functions

前端 未结 3 1865
礼貌的吻别
礼貌的吻别 2020-12-16 04:16

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

相关标签:
3条回答
  • 2020-12-16 04:58

    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 :

    <?php if (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:

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


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

    0 讨论(0)
  • 2020-12-16 05:00

    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:

    <?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:

    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);
        });
    }
    
    0 讨论(0)
  • 2020-12-16 05:03

    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

    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);
    }
    
    0 讨论(0)
提交回复
热议问题