jquery - No Ajax success after php-script is run -


i have php-script performs database backup of website. backup-script can triggered clicking button on websites' admin-panel.

i'd have text in admin-panel tells user when backup ongoing , when it's complete (process takes 1 minute) in real-time.

i believe using ajax reasonable way of doing this, , have tried following far:

<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> 

the above run backup-script fine, i'm unsure why alert doesn't trigger once script has finished running. there specific in php-file need have work?

any appreciated!

edit: added php script requested

<?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 current folder name-start     $path = dirname($_server['php_self']);     $position = strrpos($path,'/') + 1;     $folder_name = substr($path,$position);     //get 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 , save archive     $zip->close();     echo "archive created successfully.";      copy("$p.zip", "$dir/$str");     unlink("$p.zip");      /* backup db or table */     function backup_tables($mysqli, $tables = '*') {         //get of 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 $table) {             $result = $mysqli->query('select * '.$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 '.$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;     }  ?> 

your script db_backup.php should output in json format, echo this.

echo json_encode(array('success')); 

or

you can change code this:

<button id="btn">backup</button> <script>     $("document").ready(function(){         $("#btn").click(function(){             $.ajax({                 type: "post",                 url: "backup/db_backup.php",                 success: function(data) {                     alert("success!!!");                 }             });         });     }); </script> 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -