0
我有一个Ajax调用,它发布到PHP,完成后重新加载div
。首次点击失败,第二次点击在Ajax调用上成功•JQuery/PHP
按钮(阿贾克斯触发)
<div class='btn btn-danger pull-right group-action' data-action='handle=" . $folder_name . "&action=delete_group' style=' margin:5px 10px 5px 5px;'>
<i class='glyphicon glyphicon-remove'></i>
<span> Delete Group</span>
</div>
jQuery的
<script type="text/javascript">
$(document).on('click', '.group-action', function(){
var action = $(this).attr('data-action');
var randnum = Math.floor(Math.random()*1001);
$.ajax({
type: "POST",
url: "server/php/delete_action.php",
cache: false,
data: action + "&random=" + randnum,
success: function(){
$('#show_groups').load('server/php/show_groups.php');
}
});
});
</script>
PHP页面(delete_action.php)
<?php
$dir_base = 'files/';
$handle = $dir_base . $_POST['handle'];
$action = $_POST['action'];
function delete_all_images($dirname) {
foreach(glob($dirname.'/*.*') as $v){
unlink($v);
}
}
function delete_group($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_group($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
if((isset($action)) && (!empty($action)) && (isset($handle)) && (!empty($handle))){
$doit = $action($handle);
eval($doit);
}
?>
发生了什么事,当我在扳机按钮点击一次是,PHP的行为做它应该做的事情,但成功的加载函数不会触发,但是如果我点击按钮第二次,它工作正常,成功功能运行。
什么记录你在服务器端做什么?你如何跟踪你的PHP代码,看看这些值是什么以及它们是你期望的? – thescientist
我没有看到你定义的“行动”。 'data:“action =”+ action +“&random =”+ randnum,'你在哪里得到'handle'变量? – u54r
就日志记录而言,我为所有客户端操作运行了console.log()。对于服务器端来说,php正在删除我的服务器上的文件夹和文件,我在第一次点击时发现了这些文件夹和文件。 – VIDesignz