Function:

<script> function clearLogs() { if (confirm("Вы уверены, что хотите удалить логи сервера?")) { <?php if($server['server_status'] == 1): ?> <?php if(isset($_POST['btnClear'])){ unlink("путь к файлу");} ?> $.jGrowl("Логи успешно удалены!", { sticky: !1, position: "top-right", theme: "bg-green" }); //setTimeout("redirect('/servers/serverLogs/index/<?php echo $server['server_id'] ?>')", 1500); <?php elseif($server['server_status'] == 2): ?> $.jGrowl("Логи нельзя удалить на включенном сервере!", { sticky: !1, position: "top-right", theme: "bg-red" }); <?php elseif($server['server_status'] == 0): ?> $.jGrowl("11111", { sticky: !1, position: "top-right", theme: "bg-red" }); <?php elseif($server['server_status'] == 4): ?> $.jGrowl("22222", { sticky: !1, position: "top-right", theme: "bg-red" }); <?php endif; ?> return false; } else { return false; } } </script> 

If the function above is called in this way:

 <form method='post'> <button type="submit" class="btn btn-danger" name="btnClear" id="btnClear" onclick="clearLogs();">Очистить лог</button> </form> 

That, removal of logs works, BUT the page automatically reloads.

If called in this way:

 <form method='post'> <button type="submit" class="btn btn-danger" name="btnClear" id="btnClear" onclick="return clearLogs();">Очистить лог</button> </form> 

The page is not updated, but does not delete the logs!

Tell me, please, what is the matter.

Added by: enter image description here enter image description here

  • Well, cleaning up the log is not on submit, hang up, but just the input type = "button" button, or cancel the submit - preventDefault () action - Jean-Claude
  • @ Jean-Claude, I do not understand something. I did this: <input type = "button" class = "btn btn-danger" name = "btnClear" id = "btnClear" onclick = "clearLogs ();" value = "Clear log"> Nothing has changed, maybe I misunderstood you? - Max.
  • On input type = "submit" the page is overloaded, you need to cancel this standard browser action by using the preventDefault () event event learn.javascript.ru/default-browser-action or simply remove the input type = "button" button - Jean-Claude
  • one
    @Maxim, you have conditions that generate js code generated on the server. You need to make html without php , which sends ajax запрос to php , which in turn will give json answer. - Artem Gorlachev

2 answers 2

Why do you need a uniform for this? After all, you can hang an event on any element. For example:

 function clearLogs() { console.log('clearLogs'); // code } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <span class="btn btn-danger" onclick="clearLogs()">Очистить лог</span> 

Or, you need to disable the default browser action by calling the event.preventDefault () method.

 $('form').on('submit', function(event){ event.preventDefault(); console.log('clearLogs'); // code }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <form method='post'> <button type="submit" class="btn btn-danger" name="btnClear" id="btnClear">Очистить лог</button> </form> 

  • I seem stupid unrealistic, but instead of // code what should be? I after all clearLogs separate function. I'm just learning .. :) - Maxim
  • If it was necessary to transfer the code from the clearLogs function to where // code, then I did, nothing has changed, the log is not deleted. - Max.
  • @ Maxim, you need to do php on the server, and your js is running in a browser. You need to learn the basics, and then ask questions. Read books, I recommend video courses - korytoff
  • Thanks for the recommendations, for the code, but solved the problem much easier. In the form added a condition (on / off) and each condition has its own button. On off set span. In general, the result is the one that was needed. Many thanks to those who helped! - Max

 $('#clearLogs').submit(clearlogs); function clearLogs(e) { e.preventDefault(); if (confirm("Вы уверены, что хотите удалить логи сервера?")) { $.post("clearlogs.php", $(this).serialize(), function(data) { $.jGrowl(data, { sticky: !1, position: "top-right", theme: "bg-red" }); }); return false; } else { return false; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <form method="post" id="clearlogs"> <button type="submit" class="btn btn-danger" name="btnClear" id="btnClear">Очистить лог</button> </form> 

clearlogs.php:

 <?php if($server['server_status'] == 1) { if(isset($_POST['btnClear'])){ unlink("путь к файлу"); } echo "Логи успешно удалены!"; } elseif ($server['server_status'] == 2) { echo "Логи нельзя удалить на включенном сервере!"; } ... ?> 
  • It does not work, even confirm does not work, when you click on the button, it just refreshes the page and that's it. The file clearlogs.php is where the serverLogs.php screenshots the code, just in case, attached to the 1st post, did you do it right? - Max
  • see what the console writes if windows - f12 - Artem Gorlachev
  • There are no mistakes. - Maxim