0

I'm new to using AJAX methods and am completely stumped with what seems like a very simple procedure.

I'm trying to post data to a php file using AJAX in order to create a folder on my server. No matter what I try it seems as if it's not actually posting data to the php file. I can end up creating a folder directly into the 'users' folder if I remove the $_POST command from the php file... but the second I try to actually create a variable from the posted data so it creates the folder inside of a nested subfolder it fails.

please please please help. I'm losing it. haha.

Someone suggested another thread to solve the problem below... but it still doesn't seem to be working. I'm using the same approach that is suggested in that thread.

Here is my script:

JQuery

<script type="text/javascript>
    $('#buildSave').click(function() {
        $.ajax({
            url: "../php/preparesave.php", 
            type: "POST",
            data: { user : 'Tommy' }
        });
    });
</javascript>

PHP

<?php
    $user = $_POST['user'];

    if (!file_exists('../users/' . $user . '/Platoons/')) { mkdir('../users/' . $user . '/Platoons/'); } 

?>
Kaylub
  • 91
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [Ajax passing data to php script](https://stackoverflow.com/questions/6782230/ajax-passing-data-to-php-script) – Kevin Kopf Feb 24 '18 at 02:38
  • From what I can tell, I am using the approach they suggest using in that thread.... yet it still doesn't work. – Kaylub Feb 24 '18 at 02:42
  • have you ever tried adding a `success` function in your `ajax` then checking if the `post` is actually `set` in php ? – jerome Feb 24 '18 at 02:45
  • I have tried this. I receive a success command if I remove the $_POST['user'], but the post never seems to get there when I add it back. I created a simple success: function() { alert('success') }; – Kaylub Feb 24 '18 at 02:46
  • adding a `success` function to your ajax dont mean that the post have been receive. have you tried in your php something like `if(isset($_POST['user']) ? echo 1 : echo 0;` then in your `ajax` `success:function(response){ alert(response);}` something like that. because posted or not you are just alerting at the end of the ajax call – jerome Feb 24 '18 at 02:52
  • I just tried adding the if(isset) and success approach jerome suggested, but I get no alert at all when I implement it. – Kaylub Feb 24 '18 at 03:00
  • Folder write permission??? Look here: https://www.computerhope.com/unix/uchmod.htm – Louys Patrice Bessette Feb 24 '18 at 05:35

4 Answers4

3

Here is the AJAX that I suggest using

$(document).ready(function(){
  $('#save').click(function() {
    $.ajax({
      url: '../php/preparesave.php',
      type: 'POST',
      data: { user : 'Tommy' },

      success: function(output){
        alert(output);
      }
    });
  });
});

And below is the PHP (I tried it on my machine and it works)

$user = $_POST['user'];

if(!file_exists('../users/' . $user . '/Platoons/')){
  if(mkdir('../users/' . $user . '/Platoons/', 0777, true)){
    die('Success');
  }else{
    die("Folder `../users/{$user}/Platoons/` failed to be created");
  }
}

The way you have it, it will only try to create "/Platoon" in a folder $user (Tommy in your example) but that folder doesn't exist and so the script is failing. You need to set the recursive parameter to true so it would first create the folder that doesn't exist and then everything else inside it and then them.

Allows the creation of nested directories specified in the pathname. (Straight from the docs)

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
ezw
  • 338
  • 2
  • 12
  • Ladies and gents, we have a winner. Seriously Louys, you're a life saver. Absolutely top notch advice. – Kaylub Feb 24 '18 at 07:12
1

There were two errors in your code. Try to compare your lines with the one below.

 $('#buildSave').click(function() {
     $.ajax({
         url: "../php/preparesave.php", 
         type: "POST",
         data: { user : 'Tommy' }
     });
 });
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
Amir Khan
  • 11
  • 1
  • 2
  • Yea, I see that now. That was me just making 2 typos while I made my post here. My actual code doesn't have those problems. I just updated my entree to reflect these typos. Still having the same issue. – Kaylub Feb 24 '18 at 02:55
0

Try to edit your action url. Use only preparesave.php if you are calling ajax in /php folder. try to use url:"preparesave.php", if this doesnt work then use url: "preparesave.php?user=Tommy", Hope it could solve your issue.

Amir Khan
  • 11
  • 1
  • 2
  • Great suggestion. I'm calling the ajax from a JS folder (hence the ../). I know it's getting to the php file though, because the "Platoons" folder is created inside of the "Users" folder when I remove the $user = $_POST['user'] from my php file. I'm experimenting with adding ?user=Tommy to my url though, but still no success. It seems that *no* approach actually posts data to my php file. URGH! – Kaylub Feb 24 '18 at 03:07
  • try to inspect whether the ajax call is going. Then you will find the clue – Amir Khan Feb 24 '18 at 03:15
  • How would I test this? – Kaylub Feb 24 '18 at 03:16
  • press CTRL+SHIFT+I then go to network tab, then click on your custom button to call the ajax, then you will see ajax call will go to the specified action. – Amir Khan Feb 24 '18 at 03:24
  • Thank you. That was helpful. Here's the info I got from that window. I have no idea of what to do with it though. Status: 200 (green dot) Method: POST File: preparesave.php Domain: eardrumvalley.com Cause: JS xhr Type: html Transferred: 652 B Size: 0 B 280ms – Kaylub Feb 24 '18 at 03:32
  • It mean request is going on right path. Do you check the response? try to print the $_REQUEST['user'] then you will check the response as well from that window. – Amir Khan Feb 24 '18 at 23:09
0

THE SIMPLEST POSSIBLE ANSWER

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js</script>
<script>

var cars = ["Saab", "Volvo", "BMW"];

$(document).ready(function(){
  $("button").click(function(){
    $.post("test2.php",
    {
      cars
    },
    function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    });
  });
});
</script>
</head>
<body>

   <button>POST the cars array</button>

</body>
</html>

AND THE PHP.....

<?php 

$myVar = $_POST['cars']; 

$number = count($myVar);

for ($count = 0; $count < $number; $count++){
  echo $myVar[$count];
} 

?>

enter code here