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 OverflowIn 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>
The best way to stay on the same page is to post to the same page:
Copy<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
html - php, form using the same page after submittion - Stack Overflow
php Form and action in the same page - Stack Overflow
PHP FORM SUBMIT BUT STAY ON SAME PAGE
HTML FORM + PHP: form action to remain on same page as login - Stack Overflow
Videos
The other answers are right; you only need to send the user back around to your current page in the "action" property. You can test to see if they did so using "isset".
Something that I'm surprised hasn't been mentioned yet is that your input is not being sanitized, and you're setting yourself up for disaster. Huge injection vulnerability in your action attribute:
$_SERVER['PHP_SELF']
If you don't sanitize this, then someone can just modify the URL that they see and your poor PHP script won't know any better than to process that as your SELF constant.
In other words, you absolutely must use an htmlspecialchars() function to html-encode that parameter. With that, your action should look more like htmlspecialchars($_SERVER['PHP_SELF']).
if current page is index.php, use index.php in form tag as value of action.
like this:
<form action="index.php" method="post">
</form>
u can check for submitted form by putting:
if(isset($_POST)){
...
}
at top of page
You can put the code in the same file and change the form to:
<form action="" method="post">
or
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
to submit it to the same page.
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.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
You should either change method="post" in test.php or change $roomId = $_GET['roomId']; or $roomId = $REQUEST['roomId']; in results.php
Your action page is result.php not results.php,change your form to POST if you want yout code to work:
<Form name ="form1" Method ="post" Action ="result.php">
room number: <inpyt type = "text" Name ="roomId">
<input TYPE = "Submit" Name = "Submit" VALUE = "Go">
</FORM>
and in php , check first that you got value like this :
if(isset($_POST['roomId'])) $roomId = $_POST['roomId'];