php 同页处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4783381/
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
Same page processing
提问by AAA
How can process a form on the same page vs using a separate process page. Right now for signups, comment submissions, etc I use a second page that verifies data and then submits and routes back to home.php. How can I make it so that on submit, the page itself verifies rather than using a second page.
与使用单独的流程页面相比,如何在同一页面上处理表单。现在对于注册、评论提交等,我使用第二个页面来验证数据,然后提交并路由回 home.php。我怎样才能做到在提交时页面本身进行验证而不是使用第二页。
回答by Michael Irigoyen
You can tell the form to submit to the PHP's self, then check the $_POST
variables for form processing. This method is very good for error checking as you can set an error and then have the form reload with any information the user's previously submitted still in tact (i.e. they don't lose their submission).
您可以告诉表单提交给 PHP 的自我,然后检查$_POST
表单处理的变量。此方法非常适合错误检查,因为您可以设置错误,然后使用用户先前提交的任何信息重新加载表单(即他们不会丢失提交的内容)。
When the "submit" button is clicked, it will POST the information to the same page, running the PHP code at the top. If an error occurs (based on your checks), the form will reload for the user with the errors displayed and any information the user supplied still in the fields. If an error doesn't occur, you will display a confirmation page instead of the form.
当“提交”按钮被点击时,它会将信息POST到同一页面,运行顶部的PHP代码。如果发生错误(基于您的检查),表单将为用户重新加载,其中显示错误以及用户在字段中提供的任何信息。如果没有发生错误,您将显示确认页面而不是表单。
<?php
//Form submitted
if(isset($_POST['submit'])) {
//Error checking
if(!$_POST['yourname']) {
$error['yourname'] = "<p>Please supply your name.</p>\n";
}
if(!$_POST['address']) {
$error['address'] = "<p>Please supply your address.</p>\n";
}
//No errors, process
if(!is_array($error)) {
//Process your form
//Display confirmation page
echo "<p>Thank you for your submission.</p>\n";
//Require or include any page footer you might have
//here as well so the style of your page isn't broken.
//Then exit the script.
exit;
}
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?=$error['yourname']?>
<p><label for="yourname">Your Name:</label><input type="text" id="yourname" name="yourname" value="<?=($_POST['yourname'] ? htmlentities($_POST['yourname']) : '')?>" /></p>
<?=$error['address']?>
<p><label for="address">Your Address:</label><input type="text" id="address" name="address" value="<?=($_POST['address'] ? htmlentities($_POST['address']) : '')?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
回答by Jonas
The easiest construction is to detect whether the $_POST
array is not empty
最简单的构造就是检测$_POST
数组是否不为空
if(isset($_POST['myVarInTheForm'])) {
// Process the form
}
// do the regular job
回答by gaius_julius
you can check if it was POST request inside the page's code and then check the data. If it was GET request - just show the form.
您可以在页面代码中检查它是否是 POST 请求,然后检查数据。如果是 GET 请求 - 只需显示表单。
But please remember that is is a good practice to show successfull form submission results on a different page served through GET request, i.e. any successfull form POST should be answered with redirect to the success page.
但请记住,在通过 GET 请求提供服务的不同页面上显示成功的表单提交结果是一种很好的做法,即任何成功的表单 POST 都应通过重定向到成功页面来回答。
回答by diagonalbatman
You could of course explore looking into AJAX requests, where you would make an asynchronous call to your handler script, and then update then update the sending page with a success message. This gives the impression of "Same page processing" i.e. The page doesn't have to refresh.
您当然可以探索 AJAX 请求,您可以在其中对处理程序脚本进行异步调用,然后更新,然后使用成功消息更新发送页面。这给人的印象是“同一页面处理”,即页面不必刷新。
It really depends on the effect you are trying to achieve however.
但是,这实际上取决于您要达到的效果。
回答by Viral Jain
@Michael Irigoyen: It works fine, but on first rn/load, it shows:
@Michael Irigoyen:它工作正常,但在第一次 rn/load 时,它显示:
"Notice: Undefined variable: error in C:\xampp\htdocs\same_page.php on line 28"
“注意:未定义变量:第 28 行 C:\xampp\htdocs\same_page.php 中的错误”
How to handle this notice?
如何处理这个通知?
Got it now: "Used isset, @ etc. to supress errors..." "Works like a charm!!!" "Now i'll try it on my code..."
现在明白了:“使用isset,@等来抑制错误......”“像魅力一样工作!!!” “现在我要在我的代码上试一试……”
回答by PHPGuru
I have saved a thank you message and refreshed using session variables.
我保存了一条感谢信息并使用会话变量刷新。
if(!is_array($error)){
$_SESSION['message'] = 'Thank You!';
header('Location: yourpage.php');
exit();
}
and then use this in the top of the form:
然后在表单顶部使用它:
if(isset($_SESSION['message'])){
echo $_SESSION['message'];
unset($_SESSION['message'];
}
This should refresh the page and show the message and then if they refresh the page the session variable is empty so the thank you won't show. This is called a flash message.
这应该刷新页面并显示消息,然后如果他们刷新页面会话变量为空,因此不会显示谢谢。这称为快闪消息。