简体   繁体   English

PHP表单处理器错误回传

[英]PHP Form Processor Error Passback

I have a basic PHP form page that contains quite a large amount of data that will be saved into about 4-5 different tables in MySql once it is all done. 我有一个基本的PHP表单页面,其中包含大量数据,一旦完成,这些数据将保存到MySql的约4-5个不同的表中。 Since constructing this save routine will take a bit of PHP I'm looking to have the POST action to not point at PHP_SELF and instead a separate PHP file for processing. 由于构造此保存例程将需要一些PHP,因此我希望POST操作不要指向PHP_SELF,而是指向一个单独的PHP文件进行处理。

Where all general data such as phone numbers, email, zip codes, etc. will be validated prior to the submit is passed to the processor script, if an error is returned by the processor... 如果处理器返回错误,则在提交之前将所有常规数据(例如电话号码,电子邮件,邮政编码等)进行验证,然后再将其传递给处理器脚本...

What is the best practice way to point back to the original form page (HTTP_REFERER) while maintaining data input? 在保持数据输入的同时,指向原始表单页面(HTTP_REFERER)的最佳实践是什么?

Form page: 表单页面:

<form action="processor.php" action="post">
<!-- lots of fields -->
<input type="submit" id="submitButton" name="Save" value="Save" />
</form>

Processor page: 处理器页面:

<?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
          }
     }
?>

I have come across this problem before, how we solved this was to put all the fields into a session, then redirect back to form.php using header("Location: form.php"); 我之前遇到过此问题,我们如何解决此问题的方法是将所有字段放入会话中,然后使用header(“ Location:form.php”)重定向回form.php。

When the data was posted to the form, we stored the $_REQUEST into a $_SESSION['post']; 当数据发布到表单时,我们将$ _REQUEST存储到$ _SESSION ['post'];中。 if the validation failed, we sent it back to the form, populated the fields and unset the session. 如果验证失败,我们将其发送回表单,填充字段并取消会话设置。

So for example 所以举个例子

$_SESSION['post']['field_a'] = $_REQUEST['field_a'];
$_SESSION['post']['field_b'] = $_REQUEST['field_b'];

With some fancy naming conventions you can just loop this to make it easy. 有了一些精美的命名约定,您就可以循环执行此操作以使其变得容易。

Then on the Form page, we just checked to see if there was some data, or just echo the data regardless. 然后在“表单”页面上,我们只是检查是否有一些数据,或者只是回显数据。

$str_field_a = @$_SESSION['post']['field_a'];
...
<input name="field_a" value="<?php echo $str_field_a; ?>" />
...
unset($_SESSION['post']);

This is probably a messy way of doing this, but it has proven effective for our purposes. 这可能是一种麻烦的方式,但已证明对我们的目的有效。 Just thought I'd share. 只是以为我会分享。

I think you can do that using an array of error. 我认为您可以使用一系列错误来做到这一点。

  1. Set error flag false (if error occurs then set it true and so not store in database). 将错误标志设置为false(如果发生错误,则将其设置为true,因此不存储在数据库中)。

  2. Check element 1, if error then store it in array $error['name'] = 'value' 检查元素1,如果有错误,则将其存储在数组$error['name'] = 'value'

  3. Similarly check all elements, and store using same procedure. 同样,检查所有元素,并使用相同的过程进行存储。

  4. In the end if error flag is set to false do not store in database and (if on the same page, you will be able to access the array on form where you want to display error message. ) 最后,如果将错误标志设置为false,则不要将其存储在数据库中(如果在同一页面上,则可以在要显示错误消息的窗体上访问该数组。)

     if(isset($error['elementname'])) echo $error['elementname']; 

below the page. 页面下方。

However, the best approach is to use an Object Oriented approach. 但是,最好的方法是使用面向对象的方法。

[UPDATE] [更新]

  1. storing php objects on html form element and passing php objects through GET method? 在html表单元素上存储php对象并通过GET方法传递php对象?

  2. how to send redirect page pass variables as post variables from a php script 如何从PHP脚本发送重定向页面传递变量作为发布变量

Also I guess, storing the whole object in SESSION would not be a bad approach 我也猜,将整个对象存储在SESSION中并不是一个坏方法

I would send a post back to form.php containing errors and values. 我会发回包含错误和值的form.php帖子。 I use the same method in my personal project. 我在个人项目中使用相同的方法。

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
}

And this is similar to my form.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>

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

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