Situation:

After clicking on the button, you are taken to the del.php page.

<?php echo "<button onclick=\"location.href='del.php'\">Удалить файлы</button>" ?> 

Question:

How to execute PHP function without going to del.php page

PS

PHP function should be performed only after pressing the button!

Function:

 if (file_exists('./img')) foreach (glob('./img/*') as $file) unlink($file); $var = 'Файлы каталога успешно удалены'; print '<script>alert("'.$var.'");</script>' 
  • I want to add to the answer below that you should not take any actions through GET requests. Chances are good that they will give you a look at the picture with the address del.php, well, ideally use csrf token'y. - flax

3 answers 3

And I suggest not to connect extra libraries, but to use a frame.

 <a href="delp.php" target="transportFrame">Удалить файлы</a> <iframe name="transportFrame" width="1" height="1" border="0">Все плохо!</iframe> 

or so:

 <button onclick="transportFrame.location.href='del.php'">Удалить файлы</button> <iframe name="transportFrame" width="1" height="1" border="0">Все плохо!</iframe> 

And what to choose, ajax or frame - you decide.

  • Yeah, you can also generate a picture with src=del.php?file=file&rand=2823 - zb '30
  • fremail ATP But the page on which the button after deleting files does not reload. There is a list of files. How to reload the page after clicking on OK in the alert window? Here is the work page of PHP and JS pixelcom.crimea.ua/vzaimodejstvie-php-i-javascript.html - Viher
  • The easiest option is to add a script on the del.php page: print '<script> alert ("'. $ Var. '"); Self.location.reload (); </ script>'; Here are just one question that torments me: Why load a page in the background, then issue an alert, and then refresh the page? It was possible to do even without frames - the button remains, as you suggested, it goes to the del.php page, and then the alert pops up and the script redirects to the original page. Works in all browsers. If only elementary scripts worked. - fremail
  • You are right not necessarily in the background to load the page. I tried to use history.go (-1) but the page with the button does not reload. It would be nice if the script redirected to the original page. Such a script page is self.location.reload (); does not redirect to the original page with the button, it simply does not know it. If you leave the frame the alert window asks an extra question about the repetition, which is not good. How to reload the page with the "Delete files" button after clicking on OK in the alert window? - Viher
  • one
    Well history.go (-1); and should open the previous page ... There is 1 more option: we hang such a script on the button: window.location.href = 'del.php?' + window.location.toString (); And on the page del.php: alert ('Delete the file ...'); window.location.assign (window.location.search.substr (1)); I did not check, but it should work. - fremail

Use ajax . You can connect jquery (this is such a javascript library) and use its ajax function. In your case, apparently, something is removed from the server. It is generally accepted, for such purposes, to use the http delete method (you use get). To understand what I want to say by this read about rest .

     $('#button').click(function() { $.ajax({ type: 'POST', url: 'del.php', data: { del: 'Y', }, dataType: 'json', success: function(result){ alert('Ваши файлы удалены'); } }); });