I am developing a page in which on adding field data on that page i want alert which is i am putting a alert msg in php code .I want an alert in my html page. I have put alert message in php tag but it is not working.
<!DOCTYPE html>
<html>
<body>
<? if(isset($_GET['succ'])) {
$message1 = "Name is correct";
echo "<script type='text/javascript'>alert('$message');</script>";
}
if(isset($_GET['err'])) {
$message2 = "Name is not Saurabh";
echo "<script type='text/javascript'>alert('$message');</script>";
}
<form action="action.php" method="post">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page</p>
</body>
</html>
You need to use $message1
or $message2
instead of using $message.
Also, you need to close your PHP tags
before opening any HTML element(form
in your case).
Let your page URL has succ
or err
in URL parameter.
<?php
if(isset($_GET['succ'])){
$message1 = "Name is correct";
echo "<script type='text/javascript'>alert('".$message1."');</script>";// use $message1 not $message
} else if(isset($_GET['err'])){ // adding else, as URL only contains succ or err
$message2 = "Name is not Saurabh";
echo "<script type='text/javascript'>alert('".$message2."');</script>";// use $message2 not $message
}
?>
<form .....
....
....