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

?>
7
  • 1
    Possible duplicate of Ajax passing data to php script Commented Feb 24, 2018 at 2:38
  • From what I can tell, I am using the approach they suggest using in that thread.... yet it still doesn't work. Commented Feb 24, 2018 at 2:42
  • have you ever tried adding a success function in your ajax then checking if the post is actually set in php ? Commented Feb 24, 2018 at 2: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') }; Commented Feb 24, 2018 at 2: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 Commented Feb 24, 2018 at 2:52

4 Answers 4

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)

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

1 Comment

Ladies and gents, we have a winner. Seriously Louys, you're a life saver. Absolutely top notch advice.
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' }
     });
 });

1 Comment

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.
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.

6 Comments

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!
try to inspect whether the ajax call is going. Then you will find the clue
How would I test this?
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.
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
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.