簡體   English   中英

使jQuery AJAX調用特定的PHP函數

[英]Make jQuery AJAX Call to Specific PHP Functions

我是一名新手PHP開發人員。

我創建了一個PHP Web項目,其中包含一個包含“添加”按鈕的HTML頁面。 該頁面的名稱是awards.html。 awards.html文件包含其對應的JavaScript文件awards.js。 單擊“添加”按鈕時,將在此js文件中執行代碼。 此代碼將AJAX調用發送到名為Award.php的項目中位於/controller/文件夾中的PHP類。 這個PHP文件包含執行名為addFunction()的函數的代碼,該函數位於項目的/model/文件夾中的另一個Award.php文件中,該文件返回一個JSON數組並顯示在awards.html頁面中。

我的文件的源代碼如下:

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(位於/controller/文件夾中)

<?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(位於/model/文件夾中)

<?php

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

我的代碼完美無缺,並且沒有錯誤。 現在我想在awards.html中添加另一個名為Save的按鈕,單擊該按鈕將在JS文件中調用函數onSave()。 因此,我的文件的源代碼將更改為以下內容:

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);
    });
}

現在問題是因為我想從JS調用相同的Award.php文件,如何修改我的控制器Award.php文件? 在我看來,應該有兩個不同的函數,它們包含實例化視圖Award.php類的代碼並調用另一個函數; 類似以下內容:

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;
    }
}

但是如何從我的JS文件中調用這些特定的PHP函數? 如果我上面假設的代碼是正確的,那么我的JS代碼應該是什么? 任何人都可以告訴我這個嗎? 最早的回復將受到高度贊賞。 先感謝您。

好吧,我自己找到了問題的解決方案。

我發送了一個GET類型的AJAX調用,其中包含以String參數形式傳遞的數據,並添加了一個成功屬性,以確認我的代碼是否有效。 我還更改了我的Award.php代碼以成功捕獲傳遞的參數。

所以我的文件的源代碼是:

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);
}

您最好使用MVC框架,您可以在其中使用控制器功能來添加和保存功能。 您可以通過發送查詢字符串參數來告訴您的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);
}

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

你的js代碼看起來像:

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);
    });
}

我發現了一種方法。 請完整閱讀我的解釋繼續。 在你的ajax代碼中(我認為你使用jQuery),包含另一個信息'perform',value = php function name。

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

然后,在PHP文件中,在開頭添加這段代碼:

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

然后,當您使用ajax調用時,您可以執行特定的功能。 用法示例:
Javascript:

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

PHP文件example.php:

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


執行此操作時,將自動執行函數register()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM