douduan2272 2016-06-09 07:56
浏览 126
已采纳

通过ajax执行位于服务器上的php脚本

I have a website hosted on server, now what I want is to run a .php script (Also located on the same server) when someone presses the submit button in the website.

Following is the ajax code

$.ajax({url: "/test.php",
  success: function(response){
      console.log("Success",response);
    }
  });

My test.php consists of

<?php
//exec('sudo -u www-data python /var/www/html/test.py');
echo "PHP Script Ran";
mkdir("/var/www/html/test", 0700);
?>

When I navigate to ip_address/test.php, the echo message is displayed correctly but the mkdir command doesn't seem to be executed as there is no folder created in my server's directory.

Also I want to know, how can I run this test.php script when someone presses the submit button in my website.

The Javascript code is

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);
      }
    });
  });
});

Any help on this would be much appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • douji5746 2016-06-09 08:08
    关注

    In this case it's recommended to use mkdir within try...catch function and capture the error if it's the case.

    On the other hand mkidr will return a boolean value: true if the directory creation was successful or false in the case of a failure.

    1.version

    try {
        mkdir("/var/www/html/test", 0700, true);
    } catch ($ex Exception) {
        echo $ex->getMessage();
    }
    

    2.version

    if (!mkdir("/var/www/html/test", 0700, true)) {
        echo 'Failed to create folder...';
    }
    

    If mkdir cannot create the folder two things you need to check: if the folder exist and if it has the right permissions. By this i mean if the user group is set to apache (because apache, through web browser is executing the mkdir command) and second if apache (www-data) has the necessary permissions to execute this command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?