如何使用Ajax通过URL或其他方法注入特定的PHP函数?
我正在从 JS 领域转向 php 和 Ajax。我过去曾涉足过一些 PHP。 我真的非常感谢 stackoverflow 在帮助我解决基本问题方面提供的帮助。
假设我有一个名为 #divName
的 div。
我使用以下 JS 进行 Ajax。其中一些只是伪代码。
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request)
alert("Error initializing XMLHttpRequest!");
function getAjaxInfo(<name of the php function???>) { //<<<<< this is the function I need help with
var myFunction= nameOfPHPfunctionIJustGot;
var url = "filename.php?func=" + myFunction;
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works.
} else
alert("status is " + request.status);
}
}
我有我的 fileName.php 文件:
<?php
function header() { ?>
header content goes here
<?php }
function footer() { ?>
footer content goes here
<?php }
?>
我的目标是当我执行 getAjaxInfo() 时,我可以提取我想要的任何 PHP 函数。
假设我执行 onClick="getAjaxInfo(header)
" ,它将获取 php 标头函数,将其应用于 javascript 函数,然后将其应用于 div。
任何帮助将不胜感激!
I'm moving from the realm of just JS to php and Ajax. I've dabbled some with PHP in the past.
I really appreciate how much help stackoverflow has been in helping me with basic questions.
let says I have a div called #divName
.
I use the following JS for Ajax. Some of this is just pseudo code.
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request)
alert("Error initializing XMLHttpRequest!");
function getAjaxInfo(<name of the php function???>) { //<<<<< this is the function I need help with
var myFunction= nameOfPHPfunctionIJustGot;
var url = "filename.php?func=" + myFunction;
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works.
} else
alert("status is " + request.status);
}
}
The I have my fileName.php file:
<?php
function header() { ?>
header content goes here
<?php }
function footer() { ?>
footer content goes here
<?php }
?>
My goal is that when I execute getAjaxInfo()
, I can pull whatever PHP function I want.
So lets say if I do a onClick="getAjaxInfo(header)
" it will get the php header function, apply it to a javascript function, and then apply that to a div.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$allowedFunctions
是您希望 ajax 调用允许执行的用户定义(iow 您的)php 函数的白名单数组。如果您不保留此白名单,您的脚本将存在潜在危险,因为它允许任何人执行任意函数。这绝对是你不想要的。$allowedFunctions
is a whitelist array of user defined (i.o.w. your) php functions that you want the ajax call to allow to execute. If you don't keep this whitelist, your script will be potentially dangerous, since it would allow anyone to execute an arbitrary function. That is something you definately do not want.尝试:
通过这种方式,您可以获取 GET 传递的函数名称并执行它。
如果您只想调用某些函数:
Try with:
In this way you get the function name passed by GET and the execute it.
If you want to be able to call only certain functions:
采纳@VolkerK的建议,并添加一个失败函数:
Picking up on @VolkerK's suggestion, and adding a failure function: