PHP表单处理器错误回传

我有一个基本的PHP表单页面,其中包含大量数据,一旦完成,将保存到MySql中的大约4-5个不同的表中。由于构建这个保存例程需要花费一些PHP,我希望POST操作不要指向PHP_SELF而是单独的PHP文件进行处理。 如果处理器返回错误,则在提交之前将所有通用数据(如电话号码,电子邮件,邮政编码等)验证都传递给处理器脚本... 在保持数据输入的同时,指向原始表单页面(HTTP_REFERER)的最佳实践方法是什么? 表单页面:
<form action="processor.php" action="post">
<!-- lots of fields -->
<input type="submit" id="submitButton" name="Save" value="Save" />
</form>
处理器页面:
<?php
     if ( isset($_POST['date']) && ($_SERVER['HTTP_REFERER'] == "form.php") )
     {
          $errors = false;

          //attempt to put data in database

          if ( $errors )
          {
               //Pass back to the form.php page with error message and all data intact
          }
     }
?>
    
已邀请:
我之前遇到过这个问题,我们如何解决这个问题是将所有字段放入一个会话中,然后使用header(“Location:form.php”)重定向回form.php; 当数据发布到表单时,我们将$ _REQUEST存储到$ _SESSION ['post'];如果验证失败,我们将其发送回表单,填充字段并取消设置会话。 所以举个例子
$_SESSION['post']['field_a'] = $_REQUEST['field_a'];
$_SESSION['post']['field_b'] = $_REQUEST['field_b'];
通过一些花哨的命名约定,您可以循环使用以简化它。 然后在Form页面上,我们只是检查是否有一些数据,或者只是回显数据。
$str_field_a = @$_SESSION['post']['field_a'];
...
<input name="field_a" value="<?php echo $str_field_a; ?>" />
...
unset($_SESSION['post']);
这可能是一种混乱的方式,但它已证明对我们的目的有效。只是想我会分享。     
我会发回一个帖子回到包含错误和值的form.php。我在个人项目中使用相同的方法。
if ( $errors ) {
    ?><form action="form.php" method="post" name="error">
    <input type="hidden" name="errcode" value="<?php echo $errorcodes; /*or whatever message*/ ?>" />
    <input type="hidden" name="anotherdata" value="anothervalue" />
    <?php /*you can add all post datas here as hidden field*/  ?>
    </form>
    <script type="text/javascript">document.error.submit();</script><?php
}
这与我的form.php类似
//first I set default blank variables for form
$formvalue="";
$formnumericvalue="";
//i set them, yay!

//if I get values from post.php, I update the values
if (isset($_POST['iserror'])) { //you can either echo a error message or update current data here, I'm showing this for both
    $formvalue=$_POST['formvalue'];//don't forget to validate these!
    $formnumericvalue=$_POST['formnumericvalue']; //don't forget to validate these!
}
//I also do this method for edit forms

//and finally I show the form
?>
<form name="form" method="post" action="post.php">
    <input type="text" name="formvalue" value="<?php echo $formvalue; ?>" />
</form>
    
我认为你可以使用一系列错误来做到这一点。 设置错误标志为false(如果发生错误,则将其设置为true,因此不存储在数据库中)。 检查元素1,如果错误则将其存储在数组
$error['name'] = 'value'
中 同样检查所有元素,并使用相同的过程存储。 最后,如果error flag设置为false,则不存储在数据库中(如果在同一页面上,您将能够访问要显示错误消息的表单上的数组。)
if(isset($error['elementname'])) echo $error['elementname']; 
在页面下方。 但是,最好的方法是使用面向对象的方法。 [UPDATE] 将php对象存储在html表单元素上并通过GET方法传递php对象? 如何从php脚本发送重定向页面传递变量作为post变量 我想,将整个对象存储在SESSION中并不是一个糟糕的方法     

要回复问题请先登录注册