You could use JavaScript to display your alert and completely avoid making a call to the server code (php code will not get executed)
Or you could use Ajax to make an asynchronous request to the server code for processing. In this case, your php code will be called but the whole page will not be unloaded and reloaded.
Comment