Where you have:
header('location:page1.php');
append a variable on the location, like:
header('location:page1.php?status=success');
And on page1.php, do something like:
if( $_GET['status'] == 'success'):
echo 'feedback message goes here';
endif;
Answer from pbs on Stack OverflowWhere you have:
header('location:page1.php');
append a variable on the location, like:
header('location:page1.php?status=success');
And on page1.php, do something like:
if( $_GET['status'] == 'success'):
echo 'feedback message goes here';
endif;
This way your flash message will not show up again and again after refresh.
<?php session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg'] != ''){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form action="page2.php" method="post"
enctype="multipart/form-data" class="form-inline subscribe-form">
<input type="name" name="name" placeholder="Jack">
</div>
<button type="submit" name="sub" value="sub" >Submit</button>
</form>
And
<?php
session_start();
//include necessary
if(isset($_POST['sub'])) {
$nameget = mysqli_real_escape_string($dbconnect, $_POST['name']);
$sqlentry = .....bla bla......//insert into DB
}
$getsql = mysqli_query($dbconnect,
getsql){
mysql_close($dbconnect);
$_SESSIOM['msg'] = 'Value Inserted or whatever';
header('location:page1.php');
}
?>
Videos
wordpress display success message after form submit
how to add success message after form submit in wordpress
wordpress form submission success message display
try this way . try to send mail from an ajax . Please write your code like below
javascript
<script type="text/javascript">
function sendEnquiryform(){
var name=$('#name').val();
var email=$('#email').val();
var message=$('#message').val();
$.post("send_mail.php",'name='+name+'&email='+email'&message='+message,function(result,status,xhr) {
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else {
//alert(result);
$('#sucessMessage').html(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
</script>
Your html
<form method="post" name="FrmEnquiry" id="FrmEnquiry" action="" onsubmit="sendEnquiryform();">
<input name="name" id="name" required="required" placeholder="Your Name">
<input name="email" id="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" id="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
<span id="sucessMessage"> </span>
send_mail.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = '[email protected]';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail (
subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
this will display your message in your page.Please try this. This is working fine in my case.
You could post the form to the same page and check for a success message there, like this.
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = '[email protected]';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail (
subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
...other html....
<div id="message"><?php if(isset($success)){ echo $success; } ?></div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="name" required="required" placeholder="Your Name">
<input name="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
...other html....
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.
If you want just a normal pop up button, you could do the following :
$('form').on('submit',function(){
alert('submitted');
});
If you want a fancier way to do things, you can use AJAX.
You could add to your HTML a hidden div with id success, and you do the following:
<span class="success">Thank's for submitting the form</span>
and AJAX is :
$(document).ready(function() {
$("form[name='contactform']").submit(function() {
// do the extra stuff here
$.ajax({
type: "POST",
url: "mail-script.php",
data: $(this).serialize(),
success: function() {
$('.success').fadeIn(100).show();
}
})
})
})
You can give appropriate message on form submit event through below function. you can even stop form submitting through event.preventDefault(); if you mentioned inside function.
$( "form" ).submit(function( event ) {
alert( "Handler for submit() called." );
window.location.href="another html page";
});