-4

i am using the following php code to create a folder and a sub folder on xampp server...

<?php  
$userFolder = "poop";
mkdir($userFolder); 
$newfolder = $userFolder . "/bla bla bla"; 
mkdir($newfolder, 0777); 
// check to see if it has been created or not using is_dir 
if (is_dir($newfolder)) { 
    echo "The $newfolder directory has been created<br /><br />";     
}  

?>

my question is how do i use ajax to do this..... any help would be appreciated... :) thanks in advance... :)

SRC SRC
  • 87
  • 2
  • 10

2 Answers2

1

assuming your file is dir.php

<?php  
$userFolder = "poop";
mkdir($userFolder); 
$newfolder = $userFolder . "/bla bla bla"; 
mkdir($newfolder, 0777); 
// check to see if it has been created or not using is_dir 
if (is_dir($newfolder)) { 
    echo "The $newfolder directory has been created<br /><br />";     
}  

?>

you can use jquery to call it

$.ajax({
  url: "dir.php",
  context: document.body
}).done(function() {
  //sucesss response here
});
sumit
  • 15,003
  • 12
  • 69
  • 110
1

With the jQuery library (using it for brevity here), simply:

$.ajax({
  method: 'POST',
  url: 'path/to/your/php/file.php',
  data: {folderName: 'someFolderName'},
  success: function(data) {
    console.log(data); //"The directory has been created."
  } 
});

and in your php:

$userFolder = $_POST['folderName'];

jQuery isn't necessary for this. It's just less code to write. For plain javascript, see here (click).

Community
  • 1
  • 1
m59
  • 43,214
  • 14
  • 119
  • 136
  • i have used the following code ` ` – SRC SRC Nov 25 '13 at 10:06
  • and the php code `
    "; } ?>`
    – SRC SRC Nov 25 '13 at 10:07
  • @SRCSRC ...so, why did you feel the need to post all of this? – m59 Nov 25 '13 at 18:11
  • I solved your problem. I'm confused about why you made your own post using my solution rather than accepting my answer. @SRCSRC – m59 Nov 25 '13 at 18:59
  • 1
    @SRCSRC it's fine. I didn't mean you had to accept my answer, but I appreciate it. I just mean that your posted answer is redundant and poorly formatted. – m59 Nov 25 '13 at 19:13
  • well i should have accepted ur answer.... but well i was too high to think anything straight.... – SRC SRC Nov 25 '13 at 19:15