我有一個php腳本執行我的網站的數據庫備份。這個備份腳本可以通過點擊網站管理面板上的按鈕來觸發。php腳本運行後沒有Ajax成功
我想在管理面板中有一些文字告訴用戶何時正在進行備份以及何時完成(過程大約需要1分鐘)。
我相信使用Ajax是這樣做的唯一合理的方式,我已經試過到目前爲止以下:
<button id="btn">Backup</button>
<script>
$("document").ready(function(){
$("#btn").click(function(){
$.ajax({
type: "POST",
dataType: "json",
url: "backup/db_backup.php",
success: function(data) {
alert("success!!!");
}
});
});
});
</script>
上述聲明運行備份腳本就好了,但我不能確定爲什麼一旦腳本運行完畢,警報就不會觸發。有什麼具體的PHP文件,我需要有這個工作?
任何幫助非常感謝!
編輯:
var serverCall=function(){
var ajax=$.ajax({
type: "POST",
dataType: "json",
url: "backup/db_backup"
});
return ajax.then(function(data,status,xhr){
// success
}, function(){
// fail
});
}
$("#btn").on('click',serverCall);
這應該給你想要的東西:如要求
<?php
include_once '../includes/db_connect.php';
ini_set("max_execution_time", 0);
$zip = new ZipArchive();
$dir = "backups";
if(!(file_exists($dir))) {
mkdir($dir, 0777);
}
$p = backup_tables($mysqli);
echo $p;
if (glob("*.sql") != false) {
$filecount = count(glob("*.sql"));
$arr_file = glob("*.sql");
for($j=0;$j<$filecount;$j++) {
$res = $zip->open($arr_file[$j].".zip", ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile($arr_file[$j]);
$zip->close();
unlink($arr_file[$j]);
}
}
}
//get the current folder name-start
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
$folder_name = substr($path,$position);
//get the current folder name-end
$zipname = date('Y/m/d');
$str = "dRbot-".$zipname.".zip";
$str = str_replace("/", "-", $str);
// open archive
if ($zip->open($str, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$zip->addFile(realpath($folder_name . "/" . $p));
// close and save archive
$zip->close();
echo "Archive created successfully.";
copy("$p.zip", "$dir/$str");
unlink("$p.zip");
/* backup the db OR just a table */
function backup_tables($mysqli, $tables = '*') {
//get all of the tables
if($tables == '*') {
$tables = array();
$result = $mysqli->query('SHOW TABLES');
while($row = $result->fetch_array(MYSQLI_NUM)) {
$tables[] = $row[0];
}
} else {
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";
//cycle through
foreach($tables as $table) {
$result = $mysqli->query('SELECT * FROM '.$table);
$num_fields = mysqli_field_count($mysqli);
$return.= 'DROP TABLE '.$table.';';
$result2 = $mysqli->query('SHOW CREATE TABLE '.$table);
$row2 = $result2->fetch_row();
$return.= "nn".$row2[1].";nn";
while($row = mysqli_fetch_row($result)) {
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#n#","n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");n";
}
$return.="nnn";
}
//save file
$path = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$handle = fopen($path,'w+');
fwrite($handle,$return);
fclose($handle);
return $path;
}
?>
請提供您的服務器端的PHP代碼.. –
@AhosanKarimAsik,我可以在必要時提供這一點,但我相信有在那裏沒有任何與Ajax帖子相關的東西。該腳本創建一個'.sql'文件,然後將其壓縮 - 沒有別的。 – Chris
嘗試提醒(數據)。如果服務器端有任何錯誤,您可能會在警告框中看到它。事件更好去爲console.log(數據) –