tags:

views:

94

answers:

4

Hello,

I am trying to delete directories using jquery, it does delete and stays on the 1a.php, which executes the code for the delete. What I exactly want is a delete of directory without page refresh, and display a message the directory has been deleted.

Please let me know what I am doing wrong here.

Thanks in advance Dave

Please find the code below

// ----------------------------------1a.php
   if ($_POST['al_del']) {
    $dir = $_POST['al_del'];
    //preg_replace('/(\d+)/', '', $_POST['al_del']);
    rmdir('$dir_path/'.$dir);
    echo 'album deleted';
    }

//-----------------------------------1.php where the directories are listed    
    <script>
                                        $('#album_del_form').ajaxForm({
                                            target:'#dir_del',
                                            success: function() {
                                            $('#dir_del').fadeOut(40000);
                                            }
                                        });
                                </script>

                      <div style=" position:relative; top:20px; left:0px; background-color:#BBBBBB; font-family:'Arial Rounded MT Bold'; font-size:12px;">Current Albums</div>
                                <div style=" position:relative; top:20px; left:4px; font-family:'Arial Rounded MT'; font-size:12px;">
                                <? 
                                $album_path1 = "$dir_path/";
                                $cur_dir = opendir($album_path1);
                                chdir($album_path1);
                                $i_count=0;

                                while (($file = readdir($cur_dir)) !== false) {    ?>


                                <?  if (($file != ".") && ($file != "..") && (is_dir($file))){ $i_count=$i_count+1; ?>




                                <div id="dir_del">
                                <form name="album_del_form" id="album_del_form"  method="post" action="1a.php">
                                <input type="image" src="images/delete.gif" name="al_dell" id="al_dell" value="<? echo $file; ?>"/><? echo $file; ?>
                                <input type="hidden" id="al_del" value="<? echo $file; ?>" /></form>
                                </div>


                                <?  } 

                                } ?>
                                <div id="dir_del"></div>
                                </div>


                    </div>
A: 

if the dir isn't empty, rmdir will fail. you'd need to delete the files recursively. check out one of the examples at http://www.php.net/rmdir

@dave: you would see that behavior if the form plugin had a problem loading, or ajaxForm didn't get called, or the versions mismatched. i would check the result after ajaxForm and alert something after the call and take a look at console / firebug.

http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
http://jquery.malsup.com/form/jquery.form.js?2.36
jspcal
It deletes the directory, but the responsive message that I echo on 1.php is not displayed at <div id="dir_del"></div> but, goes to to the page 1a.php and stays there
Jean
what?... what really is the problem?.. I thought it was that it won't delete the directory....
Reigel
It does delete the directory. Once the delete is clicked, then it navigates to the 1a.php, which I dont want, but I need it to be without page refresh, check out the code, I am using jQuery
Jean
Did you try returning false at the end of your script to stop the page form itself from running?
Cryophallion
A: 

I think you are passing the file:

<input type="hidden" id="al_del" value="<? echo $file; ?>" />

and a1.php is expecting a directory:

$dir = $_POST['al_del'];
Reigel
$file contains the directory name, that needs to be deleted, I tried without that, but it just does not work, error is the same.
Jean
A: 

Seeing your code, I spotted an error:

<div id="dir_del">
  <form name="album_del_form" id="album_del_form"  method="post" action="1a.php">
  <input type="image" src="images/delete.gif" name="al_dell" id="al_dell" value="<? echo $file; ?>"/><? echo $file; ?>
  <input type="hidden" id="al_del" value="<? echo $file; ?>" /></form>
</div>

Given you write this code inside a while loop, there will be more than one elements like this repeated. There will be more than one form with id='album_del_form' and div with id="dir_del". Please change the way you display the form.

If I can suggest, build it like this:

<form name="album_del_form" id="album_del_form"  method="post" action="1a.php">
<?php
//...
while (($file = readdir($cur_dir)) !== false)
{
?>
  <?php
  if ( $file != "." && $file != ".." && is_dir($file) )
  {
    $i_count = $i_count + 1;
    ?>
    <div id="dir_del_<?php echo $i_count; ?>">
      <input type="image"
             src="images/delete.gif"
             name="al_del[]"
             id="al_dell_<?php echo $i_count; ?>"
             value="delete <?php echo $file; ?>"/>
      <?php echo $file; ?>
    </div>
    <?php
  }
}
?>
</form>
<div id="dir_del"></div>

And inside 1a.php, access `al_dell using array:

if ( isset($_POST['al_del']) && is_array($_POST['al_del']) )
{
  foreach ( $_POST['al_del'] as $dir )
  {
    rmdir($dir_path.'/'.$dir);
    echo 'album deleted';
  }
}

Write a code with consistent indentation will help you spot the bug and fix it quickly. Make it into habit ;)

Also, use firebug to see the error in your page. Make sure that jQuery and jQuery form is loaded, and the form is being submitted via jQuery form plugins, not using normal form submit. form submitted via jQuery form will not cause page reload or loading 1a.php, and the request and response will appear in Firebug console and network tab (if you activate it).

Donny Kurnia
Does not delete directory, it just navigates to 1a.php. which is the same error previously
Jean
Is there any error in Firebug? Have you confirmed that jQuery and jQuery form loaded to the page? If you have put it in the hosting, maybe you could put the URL here so I can check it.
Donny Kurnia
its on my local system, no I don't use firebug, for some reason it does not work ..gggrrrr...
Jean
The best part is the same code formation works at other parts
Jean
Since it's a javascript development, please consider using Firebug. You will get to know the bug in your code easily. The error displayed there will surely help you. If you don't use Firefox, Safari and Google Chrome have Web Inspector. In other browser, you can use Firebug lite.
Donny Kurnia
A: 

You need to add a return false at the end of your script to stop the form from running on the page itself after jquery handles it:

$('#album_del_form').ajaxForm({
    target:'#dir_del',
    success: function() {
    $('#dir_del').fadeOut(40000);
    };
    return false;
});
Cryophallion