Execute function without refresh and return function data

0

I have the need to run a PHP function that will generate a zip file, write to the database if it was generated and the date. And right after returning the warning that was generated.

At the moment I already have all created functions that do this. Only problem and how to do to call the function without the refresh on the page by button, update the text field with the generation date of the bank, if it was generated and the generation warning.

Below is the html part summarized to facilitate:

Arquivo gerado: <input type='text' readonly value=''>
Data de geração:<input type='text' readonly value=''>

Gerar arquivo:<input type="button" value="Gerar"/>

PHP:

class backup {
public function gera_arquivo() {
$zip = new ZipArchive();

if ( $zip->open( 'zips/arquivo.zip', ZipArchive::OVERWRITE ) === true) {
foreach( glob('Arquivos/DE/*.pdf') as $decreto )
    $zip->addFile( $decreto, $decreto);
foreach( glob('Arquivos/PO/*.pdf') as $portaria )
    $zip->addFile( $portaria, $portaria );
foreach( glob('Arquivos/LE/*.pdf') as $lei )
    $zip->addFile( $lei, $lei );
foreach( glob('Arquivos/RE/*.pdf') as $resolucao )
    $zip->addFile( $resolucao, $resolucao );
$zip->close();

}

I searched for hours on the internet, but I did not find anything that would give me a way, maybe the solution is for ajax, but I'm not very familiar with ajax.

    
asked by anonymous 21.12.2016 / 12:30

1 answer

1

You really have to use AJAX. If you want to follow an example below using JQuery you can do as follows.

Create a separate page with the function you want (I think it's more organized)

Call a page with the AJAX function and work the following:

  $.ajax({
   url:   'arquivo_com_a_funcao.php',
   type:  'POST', // DECIDA SE USARÁ POST OU GET
   data:  {geraSenha(6, true, true, true)}, //CASO A FUNCAO RECEBA PARAMETROS, PASSE ELES AQUI
   error: function() {
         alert('Erro ao tentar ação!');
   },
   success: function(resposta) { 
         alert(resposta);
   },
   beforeSend: function() { //caso queira fazer algo entre o envio e o recebimento no server
   }
});
    
21.12.2016 / 12:51