1

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

3
  • 1
    event.preventDefault(); is missing to stop the form submission. Commented Jun 9, 2016 at 8:01
  • @Jai so I added this after form.addEventListener("submit", function(event) {event.preventDefault(); ..... but how do I execute the php script now? After getting it from ajax Commented Jun 9, 2016 at 8:08
  • 1
    It seems like a permission problem: probably the script test.php hasn't the permission to add that folder. There is also the possibility that the path /var/www/html doesn't exist on the remote server. These are my ideas... Commented Jun 9, 2016 at 8:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so I did this and the response that I get is "Failed to create folder.." so probably some permission issues, I'll have a look at that Just one more thing, the command alert(response); executes the script on my server? I am a bit new to this so this part wan't clear to me.
it't a javascript action, it has nothing to do with server side. please check my edited answer.
Thanks, it works :) Had to use this guide to set the correct permissions http://fideloper.com/user-group-permissions-chmod-apache
1

Revise your php.ini in the server, the tag disable_functions = "..." and making sure that mkdir not this included in the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.