简体   繁体   English

PHP:无效表单上的刷新页面提交

[英]PHP: Refresh page on invalid form submit

How can I refresh a page with a form on submission pending the outcome of the submitted data and display a result. 如何在提交表单时刷新包含提交表单的页面,等待提交数据的结果并显示结果。

eg I have a page with a form: 例如,我有一个表格的页面:

<form action="" method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="button" name="submit" value="submit form "/>
</form>

The engine that handles the form is external, but required in the page: 处理表单的引擎是外部的,但在页面中是必需的:

require_once 'form_engine.php';

form_engine.php checks the input, form_engine.php检查输入,

$success = "true";
$errorMessage = " ";
$name = $_POST['name'];

if ( $name == '') {
      $errorMessage = 'Please enter your name';
      $success = false;
}
else (if $success = true) {
   // do something with the data

}

The form page contains the result: 表单页面包含结果:

<form action="" method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="button" name="submit" value="submit form "/>
</form>
<p><?php echo $errorMessage; ?></p>

Will the error message get displayed after the form is submitted incorrectly? 表单提交错误后是否会显示错误消息? Or do I have to use a session to store it? 或者我是否必须使用会话来存储它?

You need something like this: 你需要这样的东西:

if (!isset($_POST['name']))

instead of 代替

if ( $name == 'name')

UPDATE UPDATE

Try this, it should give you the idea: 试试这个,它应该给你的想法:

<?php

    $errorMessage = false;

    if (isset($_POST['submit'])) {

        if (!isset($_POST['name']) || $_POST['name']=='') {
            $errorMessage = 'Please enter your name';
        }
        else {
           // do something with the data
           echo "Success!!";
        }
    }
?>

<form method="post">
   <input type="name" value="" name="name" placeholder="Your Name" />
   <input type="submit" name="submit" />
</form>
<p><?php if ($errorMessage) echo $errorMessage; ?></p>

Note: leaving out the action attribute will just submit the form to the current page 注意:省略action属性只会将表单提交到当前页面

Note 2: The PHP here could very well be stored in another page. 注2:这里的PHP很可能存储在另一个页面中。 Using require() is the same as putting the code directly into the page. 使用require()与将代码直接放入页面相同。

你可以在php端使用redirect:

header('Location: www.mysite.com/index.php');

You seem to be a little confused in terms of the exact process that occurs in terms of rendering a page, as do some of those commenting. 就呈现页面而言发生的确切过程而言,您似乎有点困惑,就像其中一些评论一样。 You do not need to use sessions to solve this problem. 您不需要使用会话来解决此问题。 There is no need to store anything server-side between page requests because the user's browser with retain everything that you need, at least for this situation. 没有必要在页面请求之间存储任何服务器端,因为用户的浏览器会保留您需要的所有内容,至少在这种情况下如此。 My guess is the others took you mentioning an "external engine" and thought that the form would be submitting away to a different site/page. 我的猜测是其他人带你提到“外部引擎”,并认为该表单将提交到不同的网站/页面。


form loops 形成循环

Below is a diagram showing a typical form request loop: 下面是一个显示典型表单请求循环的图表:

一个图表,显示典型的请求流,然后呈现表单

You do not have to do this, as coding is as much about personal preference to anything else, but typically people will design their form to submit back to the same URI that generated it — as you seem to be doing in your example, by leaving the action attribute blank. 您不必这样做,因为编码与个人对其他任何事情的偏好一样多,但通常人们会设计他们的表单以提交回生成它的相同URI - 正如您在示例中所做的那样,离开action属性为空。 By doing this, as long as you embed everything you wish to pass back to the server side within the form — each time the user submits — that information will be resent and be available in PHP. 通过这样做,只要您在表单中嵌入要传递回服务器端的所有内容 - 每次用户提交 - 该信息将重新发送并在PHP中可用。

Obviously you need to be wary of what information might constitute as sensitive, as this data should only ever be written into markup if your requests are protected by HTTPS/SSL. 显然,您需要警惕哪些信息可能构成敏感信息,因为如果您的请求受HTTPS / SSL保护,则只能将这些数据写入标记。 You should also filter/escape any user input to prevent markup injection into your site. 您还应该过滤/转义任何用户输入,以防止将标记注入您的站点。 You can prevent many problems by using htmlentities , however this can cause issues depending on the values you are trying to capture from the user. 您可以通过使用htmlentities来防止许多问题,但这可能会导致问题,具体取决于您尝试从用户捕获的值。 Because you are using double quoted HTML attributes (the right way to do them ;) I have not set the ENT_QUOTES option. 因为您使用的是双引号HTML属性(正确的方法;)我没有设置ENT_QUOTES选项。


back to the point 回到原点

So in the above loop the user will be shown the form for the first time, and after any subsequent submit, which means that each time your PHP notices that there is an error you can just add your message into the page flow. 因此,在上面的循环中,用户将首次显示表单,并在任何后续提交之后,这意味着每次PHP注意到出现错误时,您都可以将消息添加到页面流中。 The trick with this kind of system is what exactly do you do once the form is fully complete. 这种系统的技巧就是在表单完全完成后你会做什么。 To get out of the loop most people will use a header location call: 要离开循环,大多数人将使用标头位置调用:

<?php

require_once 'form_engine.php';

$name = !empty($_POST['name']) ? trim($_POST['name']) : '';
$name = htmlentities($name);

if ( $success ) {
  header('location: next-step.php');
  exit;
}

?>
<form action="" method="post">
  <input type="name" value="<?php echo $name; ?>" name="name" placeholder="Your Name" />
  <input type="button" name="submit" value="submit form "/>
</form>
<?php

if ( $errorMessage ) {
  echo "<p>$errorMessage</p>";
}

?>


form engine repairs 形成发动机维修

You should also rectify your form_engine.php as per my comments above and Shekhar Joshi's answer, although I would keep the header code outside of your engine logic, and leave that decision to the code that requires in the engine — as the above does. 您还应根据我上面的评论和Shekhar Joshi的回答纠正您的form_engine.php ,尽管我会将标题代码保留在您的引擎逻辑之外,并将该决定留给引擎中需要的代码 - 如上所述。

may be, you are looking for this! 可能是,你正在寻找这个! the header() method. header()方法。

$success = true;
$errorMessage = " ";
$name = $_POST['name'];

if(isset($_POST['name'])) {
 if ( $_POST['name'] == '') {
      $errorMessage = 'Please enter your name';
      $success = false;
      header('Location: www.something.com/some.php');
 }
 else if ($success == true) {
   // do something with the data
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM