In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.

For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.

Like this:

Copy<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
  $input = $_POST['inputText']; //get input text
  $message = "Success! You entered: ".$input;
}    
?>

<html>
<body>    
<form action="" method="post">
<?php echo $message; ?>
  <input type="text" name="inputText"/>
  <input type="submit" name="SubmitButton"/>
</form>    
</body>
</html>
Answer from Tom Groot on Stack Overflow
🌐
Stack Exchange
wordpress.stackexchange.com › questions › 276445 › post-form-action-results-on-the-same-page
php - Post + form + action + results on the same page - WordPress Development Stack Exchange
August 10, 2017 - Already solved: The problem I had was to prepare a WP post with a form, then use PHP to call an external JSON service and after that present the data on the same page. I didn't need a DB. I used the "insert-php" plug-in to embed php into the page.
Discussions

html - php, form using the same page after submittion - Stack Overflow
The answer is NO. Once you submit a form -- whether you use the POST or GET method, and of course assuming that you are using $_SERVER['PHP_SELF'] or empty action, etc. -- a new page is opened automatically. This holds at least for PHP. More on stackoverflow.com
🌐 stackoverflow.com
php Form and action in the same page - Stack Overflow
It works fine but I'd like to show the result in the same page ... Sign up to request clarification or add additional context in comments. ... In addition to @ana-claudias's answer i recommend using the htmlspecialchars function to prevent XSS in case a client injects maliciuous characters into the url/variable. More on stackoverflow.com
🌐 stackoverflow.com
PHP FORM SUBMIT BUT STAY ON SAME PAGE
I have created a form that submits successfully to an email address, however, it redirects to another page with the text “Thank you. We will be in touch shortly” or “Please fill in all fields and submit again!” as I have used some simple validation. I would like the form to submit but ... More on phphelp.com
🌐 phphelp.com
0
0
December 16, 2016
HTML FORM + PHP: form action to remain on same page as login - Stack Overflow
at the moment my form links to a new page with all of my php code on it. I would like for all of the code to be executed on the same page as it does in this tutorial: http://www.w3schools.com/php/ More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
forum.freecodecamp.org › t › php-contact-form-on-the-same-page › 296722
PHP Contact Form on the same page - The freeCodeCamp Forum
July 19, 2019 - I have a contact form below and when I submit the contact form it sends me in another page saying Your Message was sent ! but I don’t want that so I need to just display a simple message inside the contact form saying Yo…
🌐
W3Docs
w3docs.com › php
PHP form - on submit stay on same page
To stay on the same page after a form submission in PHP, you can use the following steps: In the HTML form element, set the action attribute to the current page's URL, and set the method attribute to "post": <form action="<?php echo ...
🌐
HTML Form Guide
html.form.guide › php-form › php-form-action-self
Using PHP_SELF in the action field of a form | HTML Form Guide
A common use of PHP_SELF variable is in the action field of the <form> tag. The action field of the FORM instructs where to submit the form data when the user presses the “submit” button. It is common to have the same PHP page as the handler for the form as well.
Find elsewhere
🌐
Quora
quora.com › How-do-you-display-submitted-data-on-the-same-page-as-the-form-in-PHP
How to display submitted data on the same page as the form in PHP - Quora
<?php foreach ($errors as $e): ?><li><?=htmlspecialchars($e, ENT_QUOTES) ?></li><?php endforeach; ?> ... Detect submission: $_SERVER['REQUEST_METHOD'] === 'POST'. Sanitize output: always use htmlspecialchars() when echoing back to HTML. Repopulate form fields with submitted values to preserve user input after validation failure. Validation: server-side checks (and client-side for UX). Prevent double-posts: use Post/Redirect/Get (PRG) — redirect after successful processing to a results view or same page with a query flag.
🌐
PHPHelp
phphelp.com › beginners - learning php
PHP FORM SUBMIT BUT STAY ON SAME PAGE - Beginners - Learning PHP - PHPHelp
December 16, 2016 - I have created a form that submits successfully to an email address, however, it redirects to another page with the text “Thank you. We will be in touch shortly” or “Please fill in all fields and submit again!” as I have used some simple validation. I would like the form to submit but ...
🌐
Stack Overflow
stackoverflow.com › questions › 23860197 › html-form-php-form-action-to-remain-on-same-page-as-login
HTML FORM + PHP: form action to remain on same page as login - Stack Overflow
<form action="add.php" method="post"> <p> Username: <input type="text" name="Username"><br> Email: <input type="text" name="Email"><br> Password: <input type="password" name="Password"><br> Confirm Password: <input type="password" name="ConfirmPass"> </p> <p> <input type="submit"> </p> </form>
🌐
SitePoint
sitepoint.com › php
PHP form with response on same page - PHP - SitePoint Forums | Web Development & Design Community
May 6, 2015 - $('#contactForm').on('submit', function(e) { e.preventDefault(); //Prevents default submit var form = $(this); var post_url = form.attr('action'); var post_data = form.serialize(); //Serialized the form data for process.php $('#loader', form).html('<img src="../img/loader.gif" /> Please Wait...'); $.ajax({ type: 'POST', url: 'sendEmail.php', // Your form script data: post_data, success: function(msg) { $(form).fadeOut(500, function(){ form.html(msg).fadeIn(); }); } }); });
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-create-a-php-form-that-submit-to-self
How to create a PHP form that submit to self ? - GeeksforGeeks
April 28, 2025 - <form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" > ... $_SERVER['PHP_SELF']: The $_SERVER["PHP_SELF"] is a super global variable that returns the filename of the currently executing script.
🌐
YouTube
youtube.com › watch
Part 7: Show Result on Same Page after form submission in PHP - YouTube
In this video I have shown that how you can get the result of form after submission on the same page using POST method.
Published   March 1, 2021
🌐
SitePoint
sitepoint.com › php
Form process on same page - PHP - SitePoint Forums | Web Development & Design Community
February 27, 2018 - I am trying to process html form which is in file something.php. This page will have Thanks for submitting the form hidden by CSS div { display: none; } On the successful submit I will like to display this message by setting display: block for example but the problem i am finding is that on ...
🌐
W3Schools
w3schools.com › php › php_forms.asp
PHP Form Handling
However, because the variables are not displayed in the URL, it is not possible to bookmark the page. Developers prefer POST for sending form data. Next, lets see how we can process PHP forms the secure way!
🌐
PHPBuilder
board.phpbuilder.com › d › 10236260-forms-action-call-php-function-in-same-page
FORM's action call php function in same page? - PHPBuilder Forums
April 17, 2003 - Hi, can a html form's action attribute call some php code in the same page it is on? Something like this... <?php maybe some php code that always runs g...
🌐
Experts Exchange
experts-exchange.com › questions › 28169127 › php-form-on-submit-stay-on-same-page.html
Solved: php form - on submit stay on same page | Experts Exchange
June 27, 2013 - <?php error_reporting(E_ALL); ini_set('display_errors','1'); # some settings of POST vars if (!isset($_POST['send'])) $send = ''; else $send = $_POST['send']; if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText']; if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText']; if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText']; if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText']; if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText']; if (!isset($_POST['nameText'