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 - I'm trying to put on a post a custom form and, on the same page I'd like to visualize the results of an action on the form. But I'm missing the basics. Essentially I'd like to do something like this. Is it possible? <?php if(isset($_REQUEST['submit_btn'])) { echo "<div>"; $name = $_POST["names"]; echo "</div>"; } ?> <form action="" method="POST"> <input type="text" name="names" id="names"> <input type="submit" value="submit" name="submit_btn"> </form>
🌐
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.
🌐
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...
🌐
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 ...
Find elsewhere
🌐
PHP
php.net › manual › en › tutorial.forms.php
PHP: Dealing with Forms - Manual
There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called.
🌐
SitePoint
sitepoint.com › php
PHP form with response on same page - PHP - SitePoint Forums | Web Development & Design Community
May 6, 2015 - <form method="post" action="sendEmail.php" id="contactForm"> <input type="text" placeholder="Name" name="name" /> <input type="text" placeholder="Email" name="email" /> <input type="text" placeholder="Message" name="comments" /> <input type="submit" value="SEND" id="submit" name="contactSubmit" /> </form> ... $('#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(); }); } }); });
🌐
Home and Learn
homeandlearn.co.uk › php › php4p8.html
php tutorials: setting the action attribute to a different php page
You don't have to submit your form data to the same PHP page, as we've been doing. You can send it to an entirely different PHP page. To see how it works, try this: Create the following page, and call it basicForm2.php. This is your HTML. Notice the ACTION attribue.
🌐
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
to check if there has been a post to your page (this can be any page so also the same page). Basically this will check if a POST request has been send to this page. Which will if you use a from with a post method.
🌐
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.
🌐
Tech Support Guy
techguy.org › home › forums › internet & networking › web design & development
Solved: Form Action & PHP In Same Page | Tech Support Guy
If you want the php file, in the same page of the form then just leave action= empty, like so: Code: <form action=""> H · Howelly · 33 posts · Joined 2006 · Add to quote Only show this user #3 · Feb 25, 2007 · or · PHP: <? $fileName = ...
🌐
IQCode
iqcode.com › code › php › form-action-on-same-page-in-php
form action on same page in php Code Example
August 31, 2021 - <form action="<php echo $_SERVER['php_shelf'] ?>" method="POST"> <-- Your inputs here --!> </form> <?php if(isset($_POST['your input name']){ $input = $_POST['input name']; } ?> ... &lt;form action=&quot;&lt;php echo $_SERVER['php_shelf'] ?&gt;&quot; method=&quot;POST&quot;&gt; &lt;-- Your inputs here --!&gt; &lt;/form&gt; &lt;?php if(isset($_POST['your input name']){ $input = $_POST['input name']; } ?&gt;