我有一个有效的AJAX文件,它将执行PHP文件中的内容。但是,如果 php 在函数内(PHP 文件中有很多 php 功能),我如何只调用我想要的函数。
我的 AJAX 代码:
$(document).ready(function(){
// use ajax, call the PHP
$.ajax({
url: 'postme.php',
success: function(response){
$('.result_Postme').text(response);
}
})
});
Postme.php PHP 文件中的函数之一:
<?php
function echo()
{
echo 'Text';
}
?>
谢谢。
编辑:
@Amadan 嗨,我尝试了您的方法,但它似乎没有在 HTML 中输出。
我的 AjAx 代码:
$(document).ready(function(){
// use ajax, call the PHP
$.ajax({
url: 'rank.php',
data: {
action: 'echo'
},
success: function(response){
$('.rank_Alibaba').text(response);
}
})
});
PHP文件
switch($_REQUEST['action'])
{
case 'echo':
echo "text";
break;
}
.HTML:
<td class="rank_Alibaba"></td>
a) 使用 MVC 框架,例如 Laravel 或 CakePHP。这样的框架非常固执己见,无论你是否愿意,都会强加给你最佳实践:P。
b) 如果你绝对想坚持使用纯 PHP,最简单的方法是传递一个参数来选择你的函数,然后在你的 PHP 文件中有一个关于该参数的switch
语句,该语句会将调用调度到正确的位置。例如,在 JavaScript 中:
...
url: 'postme.php',
data: {
action: 'echo'
},
...
在 PHP 中:
switch($_REQUEST['action']) {
case 'echo':
echo();
break;
...
}
你需要在 PHP 文件中调用该函数:
在末尾附加一行。
<?php
function yourFunc() {
echo 'Text';
}
yourFunc()
?>
PHP 解析器怎么知道你需要从函数中执行代码。
你不能通过javascript直接调用php函数。您可以做的是类似于"调度程序"之类的事情。
$(document).ready(function(){
// use ajax, call the PHP
$.ajax({
url: 'postme.php',
data: { func: "echo" }
success: function(response){
$('.result_Postme').text(response);
}
})
});
在 PHP 中:
<?php
if($_REQUEST["func"] == "echo") {
echo();
} elseif(....) {
}
function echo()
{
echo 'Text';
}
?>
您必须根据输入查询来控制 php 行为。
将其添加到您的 php 脚本中,并且可能是有效的(只是一个示例):
<?php
if(isset($_GET['action']) && $_GET['action']) == 'echo'){
echo();
}else{
// original behaviour
}
function echo()
{
echo 'Text';
}
// whatever function else
?>
并将您的 JavaScript 更改为:
$(document).ready(function(){
// use ajax, call the PHP
$.ajax({
url: 'postme.php?action=echo',
success: function(response){
$('.result_Postme').text(response);
}
})
});
您甚至可以像这样使用 $_GET
var。
$.ajax({
url: './inc/postme.php?argument=myFunction',
type: 'POST',
data: selected
success: function(response){
$('.result_Postme').text(response);
}
});
在 PHP 方面:
if($_GET['argument'] == 'myFunction'){
//code here
}
我有 3 个文件:1-Javascript2-管理员.php3类.php(我存储函数的地方)
在java脚本中,我使用AJAX和JSON,并将对象事件(ObjEvn)发送到ADMIN.php:
//Your javaScript code
$(document).on("event", "#idElement", function(){
//Data you want to send to php evaluate
var dt={
ObjEvn:"btn_Login",//This is the controller to ADMIN.php
dataJsToPHP: $("#txt_EmailLogin").val(),
asManyasYouWant: use jQuery to grab values....
};
//Ajax
var request =$.ajax({//http://api.jquery.com/jQuery.ajax/
url: "ADMIN.php",
type: "POST",
data: dt,
dataType: "json"
});
//Ajax Done catch JSON from PHP
request.done(function(dataset){
for (var index in dataset){
dataPHPtoJsJS=dataset[index].dataPHPtoJs;
asManyasYouWantJS=dataset[index].asYouWant;
}
//JavaScript conditions. Here you can control the behaivior of your //html object, based on your PHP response
if(dataPHPtoJsJS){
$( "#idYourHtmlElement" ).removeClass( "class1" )
$( "#idYourHtmlElement" ).addClass( "class2" )
}
});
//Ajax Fail
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
现在在管理员中
.php$ObjEvn=$_POST["ObjEvn"];//Call this before all test
$asManyasYouWant = $_POST["asManyasYouWant "];
if($ObjEvn==="btn_Login"){
$login=$_POST["login"];
$passwd=$_POST["passwd"];
//call your function to test login in class.php
//finally, at the end, return your result to jquery. It could be another jquery code to //be loaded dynamiclly, it could be a html tag, it could be a value... it could be almost //everyting..
$arrToJSON = array(
"dataPHPtoJs"=>"yourData",
"asYouWant"=>"<div class='".class1'">soemting</div>"
);
return json_encode(array($arrToJSON));
}
elseif($ObjEvn==="btn_NewUser"){
//retrieve anoter values
//and call your functions to create new user
//finally, at the end, return your result to jquery. It could be another jquery code to //be loaded dynamiclly, it could be a html tag, it could be a value... it could be almost //everyting..
$arrToJSON = array(
"dataPHPtoJs"=>"yourData",
"asYouWant"=>"<div class='".class1'">soemting</div>"
);
return json_encode(array($arrToJSON));
}