A warning box or alert in PHP is a pop-up window on your computer that displays a message or information that needs the user's attention. Browsers support warning boxes, which are JavaScript dialogue boxes. PHP is a server-side language, so it does not support pop-up warning messages.
Alert Box. Use the alert() function to display a message to the user that requires their attention. This alert box will have the OK button to close the alert box. The alert() function takes a paramter of any type e.g., string, number, boolean etc.
A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
There are many problems with your script, but to answer your key question... you are echo 'ing out the javascript code for an alert() , which will in turn prevent your php header('Location:get_estimation. php'); from executing because there has already been output sent to the browser before header was called.
You could use Javascript:
// This is in the PHP file and sends a Javascript alert to the client
$message = "wrong answer";
echo "<script type='text/javascript'>alert('$message');</script>";
Create function for alert
<?php
alert("Hello World");
function alert($msg) {
echo "<script type='text/javascript'>alert('$msg');</script>";
}
?>
PHP renders HTML and Javascript to send to the client's browser. PHP is a server-side language. This is what allows it do things like INSERT something into a database on the server.
But an alert is rendered by the browser of the client. You would have to work through javascript to get an alert.
I have done it this way:
<?php
$PHPtext = "Your PHP alert!";
?>
var JavaScriptAlert = <?php echo json_encode($PHPtext); ?>;
alert(JavaScriptAlert); // Your PHP alert!
You need some JS to achieve this by simply adding alert('Your message')
within your PHP code.
See example below
<?php
//my other php code here
function function_alert() {
// Display the alert box; note the Js tags within echo, it performs the magic
echo "<script>alert('Your message Here');</script>";
}
?>
when you visit your browser using the route supposed to triger your function_alert
, you will see the alert box with your message displayed on your screen.
Read more at https://www.geeksforgeeks.org/how-to-pop-an-alert-message-box-using-php/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With