Raz*_*zor 8 html javascript php ajax jquery
我是一名新手PHP开发人员.
我创建了一个PHP Web项目,其中包含一个包含"添加"按钮的HTML页面.该页面的名称是awards.html.awards.html文件包含其对应的JavaScript文件awards.js.单击"添加"按钮时,将在此js文件中执行代码.此代码将AJAX调用发送到位于/controller/名为Award.php的项目文件夹中的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>
Run Code Online (Sandbox Code Playgroud)
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);
});
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
Award.php(位于/model/文件夹中)
<?php
class Award
{
public function addFunction() {
$array = array(
'status' => '1'
);
return $array;
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码完美无缺,并且没有错误.现在我想在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>
Run Code Online (Sandbox Code Playgroud)
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);
});
}
Run Code Online (Sandbox Code Playgroud)
现在问题是因为我想从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);
}
Run Code Online (Sandbox Code Playgroud)
Award.php
class Award
{
public function addFunction() {
$array = array(
'status' => '1'
);
return $array;
}
public function saveFunction() {
$array = array(
'status' => '2'
);
return $array;
}
}
Run Code Online (Sandbox Code Playgroud)
但是如何从我的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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31342 次 |
| 最近记录: |