0

i have following code:

if(isset($_POST['Button']) && (!empty($_POST['Button']))){
    echo '<script language="JavaScript">alert("ALERT MESSAGE");</script>';
    echo $classFunctions->Function($db, $_POST);
    echo '<meta http-equiv="refresh" content="0;URL=\'refertarget\'">';
}

but the alert message does not appear before the function is executed. Any suggestions how i can handle that alert message appears before the function will be executed?

thanks and regards

Manuel Weitzel
  • 101
  • 1
  • 1
  • 13

1 Answers1

0

This cannot be done in PHP itself. Since PHP is executed on the server, builds the page and then sends it to the client where the JavaScript is executed.

To achieve something close to what you're trying to do would require you to send another request to the PHP file when the alert is closed, which does then execute the function:

<script>
  alert('ALERT');
  fetch('url/to/php-file');
</script>

remember that there is also confirm instead of alert where you can select Yes or No and only proceed on Yes:

<script>
  if (confirm('PLEASE CONFIRM')) {
    fetch('url/to/php-file');
  }
</script>
Linschlager
  • 1,539
  • 11
  • 18
  • you mean when i use confirm instead of alert it would work? i think i understood wrong - it would not work i think because i don't have a php-file to fetch. I just have Functions all in one php-file and can't fetch this - because it needs to be called as i posted in the question – Manuel Weitzel May 12 '20 at 09:06
  • okay, danke auf jeden Fall, Dein Tipp hat mir bei der Lösung geholfen :-) Ich habe den alert jetzt einfach in einer Javascript Funktion verpackt und auf "form onsubmit" rufe ich diese Funktion auf, somit erscheint in ersten Tests das Alert bevor die Funktion ausgeführt wird – Manuel Weitzel May 12 '20 at 09:31