Reputation: 9701
I'm creating a class to create and delete folders and some other things I still have to work on. I'm doing this via ajax. And it works fine. But I get the same message in the console twice.
Why is that? Am I doing something wrong?
Here is my code till now (also available at http://snippi.com/s/3sfsfvm):
<?php
//process.php
if(!empty($_POST['foldddername'])) {
require_once 'test.php';
$folder = new folder;
$path = dirname( __FILE__ );
$foldername = strip_tags($_POST['foldddername']);
// $folder ->crtFolder($foldername,$path);
if($message = $folder ->crtFolder($foldername,$path)) {
echo $message;
}
}
?>
<?php
//test.php
class folder
{
public function crtFolder($foldername,$path){
$dirpath = $path."\\".$foldername;
if ((!is_dir($dirpath))) {
if(mkdir($dirpath,0777,true)) {
$error = false;
$message['error'] = false;
$message['message'] = "Folder Created";
return json_encode($message);
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Folder Failed To Create";
return json_encode($message);
}
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "Folder Already Exists";
return json_encode($message);
}
}
}
?>
//Ajax handling
$(function(){
$('.submittt').click(function(){
if($('input.folder-name').val() == "")
{
console.log('Please enter Folder Name');
return false;
}
else
{
$.ajax
({
type: 'POST',
url: 'process.php',
dataType: 'json',
data:
{
foldddername: $('input.folder-name').val()
},
success:function(data)
{
console.log(data.message);
if(data.error === true)
{
console.log(data.message);
}
else
{
console.log(data.message);
}
},
error:function(XMLHttpRequest,textStatus,errorThrown)
{
console.log(data.message);
}
});
return false;
}
});
});
Upvotes: 0
Views: 332
Reputation: 33217
this part of code produces duplicates:
success:function(data) {
console.log(data.message); # 1st time
if(data.error === true) {
console.log(data.message); # duplicate
}
else {
console.log(data.message); # duplicate
}
},
Upvotes: 2