Run function without refresh and return function data

Asked

Viewed 695 times

0

I 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 call the function without the refresh on the page per button, update the text field with the database generation date, if it was generated and the generation warning.

Below is the part of the short html to make it easier:

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 to give me a way, maybe the solution is by ajax, but I am not very familiar with ajax.

1 answer

1


Actually, you will 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 this is 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
   }
});
  • I am adapting the example, I am advancing slowly. Difficulty sending a return to a box text once completed.

  • An input text? Just in the Success block put $("#id_do_input"). val('desired value');

  • It worked. Another question, how do I take a data from a php function variable and return to ajax?

  • You have some knowledge for that?

  • As this function is in the code?

  • The most "easy" way would be var x = <?php echo $variavel; ? > @Williancoqueiro

  • Your reply helped me find a way. I made many changes that resulted in what I wanted. Thank you very much.

  • @Williancoqueiro Whatever you need, we are at your disposal :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.