After long researching, I found solution to delay the alert message with jQuery delay() function, the delay allowing the HTML page load and then execute the alert.
Thanks to all who helped me to get to this result.
<script>
$(document).ready(function(){
setTimeout(function() {
<?php
if( $_GET['status'] == 'success') {
echo 'alert("welldone");';
}
else{
echo 'alert("no good");';
}
?>
}, 500);
});
</script>
Answer from Yotam Dahan on Stack OverflowAfter long researching, I found solution to delay the alert message with jQuery delay() function, the delay allowing the HTML page load and then execute the alert.
Thanks to all who helped me to get to this result.
<script>
$(document).ready(function(){
setTimeout(function() {
<?php
if( $_GET['status'] == 'success') {
echo 'alert("welldone");';
}
else{
echo 'alert("no good");';
}
?>
}, 500);
});
</script>
alert() stops the execution of the script and the loading of the HTML page at the point in which it was called. You should move this script to the bottom of the page.
Videos
pagewithform.php
<html>
<head>
...
</head>
<body>
...
<form action="myformsubmit.php" method="POST">
<label>Name: <input type="text" name="name" /><label>
<input type="Submit" value="Submit" />
</form>
...
</body>
</html>
myformsubmit.php
<html>
<head>
....
</head>
<body>
<?php if (count($_POST)>0) echo '<div id="form-submit-alert">Form Submitted!</div>'; ?>
...
</body>
</html>
EDITED Fits new critieria of OP on last edit.
EDITv2 Try it at home!
<html>
<head>
<title>Notify on Submit</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label>Name: <input type="text" name="name" /></label>
<input type="submit" value="Submit" />
</form>
<?php if (count($_POST)>0) echo "Form Submitted!"; ?>
</body>
</html>
Try that on for size.
Since you're submitting back to the same page, a cleaner and more modern way of doing this would be to use JQuery to submit the form using AJAX. You can then specify a callback method that will update a container on the page to reflect the change in state:
$('#myForm').submit(function() {
$('#myResultDiv').text("Form submitted");
return false;
});
...
<div id="myResultDiv"></div>
This prevents the unnecessary reloading of the page, making your web application snappier and more responsive.
This also has the added benefit of keeping your HTML and JavaScript (content and behavior) separate, for which your web designers will thank you for.
This would work with just about any server-side platform, including but not limited to PHP.
Actually you are displaying the message in view ; this part of your code is sufficient for that.
<div class="col-md-10 text-center" id="success" style="display:none">
<?php if(isset($message)) echo $message ?>
</div>
But the div has style display:none so it will not be displayed. So change it to
<?php if(isset($message))
{
?>
<div class="col-md-10 text-center" id="success">
<?php echo $message ?>
</div>
<?php
}
?>
if you want an alert message then you can change it to
<?php if(isset($message))
{
?>
<script>
alert('<?php echo $message ?>');
</script>
<?php
}
?>
If it is an javascript alert, search for the alert expression with the alerted message, put an . And use console.log()...
You are echoing outside the body tag of your HTML. Put your echos there, and you should be fine.
Also, remove the onclick="alert()" from your submit. This is the cause for your first undefined message.
<?php
$posted = false;
if( $_POST ) {
$posted = true;
// Database stuff here...
// $result = mysql_query( ... )
$result = $_POST['name'] == "danny"; // Dummy result
}
?>
<html>
<head></head>
<body>
<?php
if( $posted ) {
if( $result )
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
else
echo "<script type='text/javascript'>alert('failed!')</script>";
}
?>
<form action="" method="post">
Name:<input type="text" id="name" name="name"/>
<input type="submit" value="submit" name="submit"/>
</form>
</body>
</html>
Instead of using a submit button, try using a <button type="button">Submit</button>
You can then call a javascript function in the button, and after the alert popup is confirmed, you can manually submit the form with document.getElementById("form").submit(); ... so you'll need to name and id your form for that to work.