我已经搜索了将近一个小时,但没有找到合适的示例来解决我的问题: 我想调用一个 PHP 函数(这是一个简单的取消链接,与在页面加载时执行的 javascript 函数给出的路径)。我对 AJAX 一点都不擅长,我想了解如何从 javascript 代码直接调用 index.php 文件中包含的 PHP 函数。
这是我的 javascript 代码片段中的内容:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/dev/templates/absolu/index.php?s=" + Math.random(),true);
//@unlink(listeImages[i].toString());
最佳答案
您发送函数名称(以防您将来会有更多函数)和参数作为获取参数
var fileToDelete;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/dev/templates/absolu/index.php?s=" + Math.random()+"&action=delete&file="+fileToDelete,true);
在您的 PHP 脚本中,您应该处理它:
<?php
if (isset($_GET['action'])){
switch($_GET['action']){
case 'delete':
@unlink(listeImages[$_GET['action']].toString());
break;
//Other functions you may call
}
exit;
}
//The rest of your index.php code
?>
关于php - 从 javascript 调用 php 函数,onload(AJAX?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11208840/