让jQuery的AJAX调用特定的PHP函数 [英] Make jQuery AJAX Call to Specific PHP Functions

查看:135
本文介绍了让jQuery的AJAX调用特定的PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是菜鸟PHP开发人员。

I am a rookie PHP developer.

我创建了一个PHP Web项目,其中包含一个添加按钮的HTML页面。页面名称是awards.html。该awards.html文件中包含了对应的JavaScript文件,awards.js。 A code在此执行js中单击Ad​​d按钮时文件。这code发送一个AJAX调用到位于 /控制器/ 文件夹命名,Award.php项目中的PHP类。这其中包含code PHP文件,在位于另一个Award.php文件执行调用,addFunction()函数/模型/ 文件夹中的项目,它返回一个JSON数组和显示在awards.html页

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 its counterpart JavaScript file, awards.js. A code is executed in this js file when the Add button is clicked. This code sends an AJAX call to a PHP class located in /controller/ folder in the project named, Award.php. This PHP file which contains code to execute a function called, addFunction() in another Award.php file located in /model/ folder in the project, which returns a JSON array and is displayed in the awards.html page.

我的文件的源$ C ​​$ C给出如下:

The source code of my files is given 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(位于 /控制器/ 文件夹)

Award.php (located in /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(位于 /模型/ 文件夹)

Award.php (located in /model/ folder)

<?php

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

我的code完美的作品,并没有错误。现在,我想补充的awards.html另一个按钮调用,保存,点击后调用一个函数的OnSave()中的JS文件。因此,我的文件的源$ C ​​$ C将变更为以下内容:

My code works perfectly and is error-free. Now I want to add another button in awards.html called, Save which when clicked will call a function onSave() 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);
    });
}

现在这里的问题是,因为我要调用JS来自同一Award.php文件,我怎么修改我的控制器Award.php文件?在我看来,应该是twoo不同的功能,其中将包括code实例化视图Award.php类,并调用不同的功能;类似如下:

Now the problem here is that since I want to call the same Award.php file from JS, how do I modify my controller Award.php file? In my opinion there should be twoo different functions which would contain code to instantiate the view Award.php class and call a different 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;
    }
}

但我怎么叫从我的JS文件中的这些具体的PHP函数?如果code我在上面已经假设是正确的,那么应该是什么我的JS code?任何人都可以请告诉我这个?回复最早将高度AP preciated。谢谢你在前进。

But how do I call these specific PHP functions from my JS file? If the code I have assumed above is correct, then what should be my JS code? Can anyone please advise me on this? Replies at the earliest will be highly appreciated. Thank you in advance.

推荐答案

好吧,我找到了解决自己的问题。

Okay I found the solution to the problem myself.

我派在字符串参数的形式传递的数据一个GET型AJAX调用,并增加了一个成功的属性来得到一个确认我的code是否曾与否。我也改变了我的Award.php code,成功搭上传递的参数。

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.

所以,我的文件的源$ C ​​$ c是:

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

这篇关于让jQuery的AJAX调用特定的PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆