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'];?>">
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">
1>Change the value of the action attribute of form tag as "action?submitfunc"
2>and check through get value
As follows :
if(isset($_GET['action'])=='submitfunc') {
submitfunc();
}else
//show form
?>
<body>
<form action="?action=submitfunc" method="post">
if(isset($_POST['submit'])) {...
<form action="" method="post">
...
<input type="submit" value="Wyślij" name="submit"/></p>
...
This line is searching for an object with the name submit on it
if(isset($_POST['submit']))
{
But your form doesn't have such object, so add a name to your input and the function shall work:
<input type="submit" name="submit" value="Post" class="col-md-2" id="quick-post-submit">
<input type="submit" value="Post" class="col-md-2" id="quick-post-submit">
vs
if(isset($_POST['submit']))
{
quick_post();
}
you didn't give the submit button a name="submit" attribute
A very simple way to do is to do following :
yourpage.php
<?php
if(isset($_POST)){
//data posted , save it to the database
//display message etc
}
?>
<form method="post" action="yourpage.php" >....
You can do a redirect in php, to the html form - and you can set a "flash message" - to show "instance added" by saving "instance added" to the session and showing that value when you redirect to html.
As others have said, you can't direct a post to a function automatically, but you can dynamically decide what to do on the PHP side depending on which form is submitted with PHP code. One way is to define your logic with a hidden input so you can handle different actions on the same page, like this:
<form name="tryLogin" action="index.php" method="post">
<input type="hidden" name="action" value="login" />
Username: <input type="text" name="username" placeholder="username.."><br />
Password: <input type="text" name="password" placeholder="password.."><br />
<input type="submit" value="Submit">
</form>
<form name="otherform" action="index.php" method="post">
<input type="hidden" name="action" value="otheraction" />
Type something: <input type="text" name="something"><br />
<input type="submit" value="Submit">
</form>
and then in your PHP:
if (isset($_POST['action'])) {
switch($_POST['action']) {
case 'login':
login();
break;
case 'otheraction':
dosomethingelse();
break;
}
}
No you submit the form to a page and you run your function if the form is submitted:
Html:
<form action="index.php" method="post">
PHP (index.php):
if ($_SERVER['REQUEST_METHOD'] == "POST"){
// Run your function
login();
}