相同的页面处理

AAA*_*AAA 7 php verification process onsubmit

如何在同一页面上处理表单与使用单独的流程页面.现在注册,评论提交等我使用第二页验证数据,然后提交并路由回home.php.我怎样才能使它在提交时,页面本身验证而不是使用第二页.

Mic*_*yen 14

您可以告诉表单提交给PHP的self,然后检查$_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>
Run Code Online (Sandbox Code Playgroud)

  • @AAA我不明白你的问题.上面的方法是我在90%的表单中使用的确切模型,并且几乎是@NeqO提供的扩展示例.您想要在"刷新"页面方面做些什么? (2认同)

Jon*_*nas 7

最简单的构造是检测$_POST数组是否为空

if(isset($_POST['myVarInTheForm'])) {
  // Process the form
}

// do the regular job
Run Code Online (Sandbox Code Playgroud)