php,表单提交后使用同一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15130493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php, form using the same page after submittion
提问by Dora
I'm wondering what's the easiest way to make let's say a form with user/pass and submit button with php but after you submit it goes back to the same page instead of going to another php page.
我想知道什么是最简单的方法,让我们说一个带有 user/pass 的表单并使用 php 提交按钮,但是在您提交后它会返回到同一页面而不是去另一个 php 页面。
I'm thinking of if/else statement but a bit confused how to set it after lots tries but still not getting the result wanted
我正在考虑 if/else 语句,但有点困惑如何在多次尝试后设置它但仍然没有得到想要的结果
weird I did all those you guys said before I posted..but...>.<"let's say just something simple like this...
很奇怪,我在发布之前做了你们说的所有事情......但是......>.<”让我们说一些简单的事情......
but I also want to set if nothing is entered then sumbit is clicked there'll be an error....should be something easy but I don't know why I can't seem to figure it out
但我也想设置如果没有输入任何东西然后点击 sumbit 就会出现错误......应该很容易但我不知道为什么我似乎无法弄清楚
<?php
function userPass()
{
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />";
}
if(empty($_POST["user"]))
{
userPass();
}
if(!(empty($_POST["user"])))
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
回答by forresthopkinsa
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".
其他答案是正确的;您只需要将用户发送回“ action”属性中的当前页面。您可以使用“ 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. Hugeinjection 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 SELFconstant.
如果你不清理这个,那么有人可以修改他们看到的 URL,你可怜的 PHP 脚本不会比将它作为SELF常量处理更好。
In other words, you absolutely mustuse an htmlspecialchars()function to html-encode that parameter. With that, your actionshould look more like htmlspecialchars($_SERVER['PHP_SELF']).
换句话说,您绝对必须使用一个htmlspecialchars()函数来对该参数进行 html 编码。有了这个,你action应该看起来更像htmlspecialchars($_SERVER['PHP_SELF']).
回答by codefreak
if current page is index.php, use index.php in form tag as value of action.
如果当前页面是 index.php,在表单标签中使用 index.php 作为 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
在页面顶部
回答by Techie
Just use below syntax
只需使用以下语法
<form method="post" action="">
You can check whether post is set using isset() method.
您可以使用isset() 方法检查是否设置了post。
回答by Boris
This is a code that I created to control learning the required input fields satisfy the requirements. when this is so, the data will be sent to the database. if it does not meet the requirements, there will be a message may be shown at the top of the page
这是我创建的代码,用于控制学习所需的输入字段是否满足要求。如果是这样,数据将被发送到数据库。如果不符合要求,可能会在页面顶部显示一条消息
<?php
if (!isset($_POST['submitform'])) {
}
else
{
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['place'] = $_POST['place'];
$_SESSION['street'] = $_POST['street'];
$_SESSION['housenumber'] = $_POST['housenumber'];
$_SESSION['gender'] = $_POST['gender'];
if (empty($_POST['firstname']) or empty($_POST['lastname']) or empty($_POST['email']) or empty($_POST['mobile'])or empty($_POST['telephone']) or empty($_POST['place'])
or empty($_POST['street']) or empty($_POST['housenumber']) or !isset($_POST['gender']))
{
echo "Sending denied";
}
else
{
require 'database.php';
header('Location: succes.php');
} ?>
I hope this is helpful information for you
我希望这是对你有用的信息
回答by Axel Capaert
<?php function userPass() {
echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
echo "<input type='text' name='user' /><br/>";
echo "<input type='text' name='pass' /><br/>";
echo "<input type='submit' value='Login' />"; }
if(empty($_POST["user"]))
{
userPass();
}
*if(!(empty($_POST["user"])))*
{
if($_POST["user"] == "comp")
{
echo "Welcome comp";
}
else
{
echo "Wrong user";
}
}
?>
This part of your code is wrong, you should type : if(!empty($_POST["user"]))
这部分代码是错误的,你应该输入: if(!empty($_POST["user"]))
回答by Jei
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
This is exactly how you work with your form to reload the page when click the submit button inside this form.
这正是您在单击此表单内的提交按钮时使用表单重新加载页面的方式。
ADDITIONAL:Add requiredto all input you wanted to be required or check if it's empty.
附加:添加required到您想要的所有输入或检查它是否为空。
回答by Ehsan Ilahi
this code will help you
此代码将帮助您
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
or you could just do this:
或者你可以这样做:
<form action="" method="post">
Thank you ....
谢谢 ....
回答by Apostolos
I think that all have missed the actual question, i.e. if there is a way to stay in the same pageafter submitting a form. 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. (I have tried a lot of different ways using XAMPP.) I don't now about other scripting languages.
我认为所有人都错过了实际问题,即是否有办法在提交表单后停留在同一页面。答案是否定的。一旦您提交表单——无论您使用 POST 还是 GET 方法,当然还假设您使用的是 $_SERVER['PHP_SELF'] 或空操作等 -一个新页面将自动打开。这至少适用于 PHP。(我使用 XAMPP 尝试了很多不同的方法。)我现在不了解其他脚本语言。
回答by yourdeveloperfriend
You can define in the form submission:
您可以在表单提交中定义:
<form method=get action=index.php >
You can also leave action out altogether, and it will automatically post/get to that same file.
您也可以完全忽略操作,它会自动发布/获取到同一个文件。

