我有一个网站托管在服务器上,现在我想要的是当有人按下网站上的提交按钮时运行.php脚本(也位于同一台服务器上)。
通过AJAX执行位于服务器上的PHP脚本
以下是Ajax代码
$.ajax({url: "/test.php",
success: function(response){
console.log("Success",response);
}
});
我的test.php由
<?php
//exec('sudo -u www-data python /var/www/html/test.py');
echo "PHP Script Ran";
mkdir("/var/www/html/test", 0700);
?>
当我浏览到IP_ADDRESS/test.php的,正确显示echo
消息,但mkdir
命令似乎没有执行,因为在我的服务器目录中没有创建文件夹。
另外我想知道,当有人按下我网站上的提交按钮时,我该如何运行这个test.php脚本。
的JavaScript代码是
var $ = jQuery;
var timestamp = Number(new Date());
var form = document.querySelector("form");
var database = firebase.database();
form.addEventListener("submit", function(event) {
var ary = $(form).serializeArray();
var obj = {};
for (var a = 0; a < ary.length; a++) obj[ary[a].name] = ary[a].value;
console.log("JSON",obj);
firebase.database().ref('users/' + timestamp).set(obj);
database.ref('users/' + timestamp).once('value').then(function(snapshot) {
console.log("Received value",snapshot.val());
$.ajax({
url: "/test.php",
success: function(response){
console.log("Success",response);
}
});
});
});
任何帮助,在此,将不胜感激。 谢谢
'event.preventDefault();'缺少停止表单提交。 – Jai
@Jai所以我在'form.addEventListener(“submit”,function(event){event.preventDefault(); .....'后添加了这个,但是我现在怎么执行php脚本呢? 从ajax –
这似乎是一个权限问题:可能脚本'test.php'没有权限添加该文件夹,也有可能是'/ var/www/html'路径在远程服务器上不存在。这些是我的想法... – gior91