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>
&l开发者_JAVA技巧t;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>
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'];
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
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).
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;
});
精彩评论