【问题标题】:Using PHP to request errors information and post if errors has anything使用 PHP 请求错误信息并发布错误信息
【发布时间】:2011-03-17 00:06:26
【问题描述】:

我有一个 HTML 表单,它接受输入的数据并通过 mail() 函数发送它。我还有一些验证输入的验证技术,我创建了一个数组变量 $errors 来记录所有错误;例如,

如果名称为空,$errors[]="Name empty"; 如果电子邮件为空,$errors[]="email empty";

等等..

我能够使用以下技术报告错误:

print '<div id="formfeedback"><h3>Error!</h3><p>The following error(s) has occurred:<br />';
    foreach ($errors as $msg) { //prints each error
            print " - $msg<br />\n";
        } // end of foreach

但是,我想要的是以下内容。我希望将页面重定向回用于输入信息的原始表单(我知道确切的链接位置,因此我可以使用 header() 甚至 &lt;meta=http-equiv=refresh&gt; 将我带回表单页面。

另外,在表单上,​​我希望能够在某个 div 中发布表单上方的错误(称为 div=errors)

我能做到以下几点吗?

    <div id="errors">
<?php    
print 'The following error(s) has occurred:<br />';
            foreach ($_REQUEST[$errors] as $msg) { //prints each error
                    print " - $msg<br />\n";
                } // end of foreach
?>   
 </div>

非常感谢!

阿米特

【问题讨论】:

  • 哦,最后一件事,此表单位于 wordpress 页面上。如何将 PHP 输入到 wordpress 页面?即使我使用带有 raw_html 插件的 HTML 编辑器,它也会以段落形式输入它?

标签: php validation forms


【解决方案1】:

我同意@Fosco。我想多解释一点-

可能有两种情况- 1.你在做原始的php 2. 您正在编写任何 php 框架,例如 CI 或您自己的。

这将有助于识别错误字段并更改样式以做出更好的用户响应。最后输入的数据也保持原样。

  1. 你正在做原始的 php 在这种情况下,您可以在同一文件/页面中接收输入数据。 后面我会做一个常见的例子。

  2. 您正在使用任何 php 框架(如 CI 或您自己的框架)进行编码。 在这种情况下,您加载视图文件以显示表单页面,并且您可以在加载时将数据传递给视图页面/文件。

对于上述两种情况,您都可以进行一些编码,例如-

/* 
your input validation and verification goes here. where $error is generated too
In addition add some error status in above section, 
you can do it in your $error array too. Also you store received data into $data here. index of $data should be similar as (corresponding) HTML input name. 
You can do it like below
*/
$error_stat = array();
//if the input field name is "email" and email input data raises any error then

$error_stat['email'] = true;
// same for name
$error_stat['name'] = true;
// and so on

// now decide whether you will back to the form page or send the email and do other tasks
if(count($error_stat)<= 0){
  // send email
  // do aditional tasks
}
else{
  // load the form again if its aframework or the form is in seperate file
  // off course send $error,$data and $error_stat to the form page/file
}

// now here is a code segment of form page
<?php if(isset($error) && count($error)>0):?>
<div id="error-msg">
<?php
//display errors here
?>
</div>
<?php endif;?>

<form .... >
<input type="text" name="email" class="<?php echo (isset($error_stat['email'])?'error':'else'); ?>" value="<?php echo $data['email'];?>" />\
<!-- and so on ... -->

【讨论】:

    【解决方案2】:

    最简单的方法是:

    // Start the session
    session_start();
    
    // Store the errors in the session
    $_SESSION['errors'] = $errors;
    
    // Redirect to correct page
    header('HTTP/1.1 303 See Other');
    header('Location: http://original/page');
    exit;
    

    然后,在表单页面上:

    // Start the session
    session_start();
    
    // extract the errors
    $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : array();
    
    // Display the form with errors
    foreach ($errors as $msg) ... ;
    

    【讨论】:

    • 这看起来不错。我想知道,下面的标题是做什么的? header('HTTP/1.1 303 查看其他');我曾经有过 header() 没有真正重定向我的时候,可能是因为我错过了那条线吗?
    • 默认情况下,header('Location: ...') 使用 302 HTTP 状态,根据 HTTP 的某些解释,这会导致浏览器向新位置提交相同的请求(在此情况下,具有相同变量的 POST)。使用 303 会导致它执行 GET,丢弃任何 POST 参数。
    • @Nillocet:虽然这会很好,但这会使 $_SESSION 变得更重,所以这不是一个好习惯。
    • @Saadt:是的。通常,表单数据在显示时会从会话中删除,因此内存只会在用户不遵循重定向时“泄漏”。
    • 是否可以使用 wordpress 页面运行?
    【解决方案3】:

    通常我会在同一个页面处理输入和提交。如果数据有效,则会发送邮件并且页面会通知他们(或将他们重定向到其他地方)......如果数据无效,那么表单将再次出现并且可以显示错误,没有任何花式重定向。

    【讨论】:

    • 重定向用户有助于避免刷新页面再次提交数据的问题。另外,您可以将表单显示分离到控制器A中,将表单解析到控制器B中。如果您的用户在注销后提交表单(重定向用户登录:用户登录,返回表单,表单已填写),这也很有用因为数据在 $_SESSION 中)。
    【解决方案4】:

    确保您的会话从应用程序的顶部开始

    包括这个基本类

    class FormErrors
    {
        var $handler;
        function __construct($fname)
        {
            $this->handler &= isset($_SESSION[$fname]) $_SESSION[$fname] : ($_SESSION[$fname] = array());
        }
    
        public function add($name, $value)
        {
            $this->handler[$name] = $value;
        }
    
        public function remove($name)
        {
            unset($this->handler[$name]);
        }
    
        public function getErrors()
        {
            return $this->handler;
        }
    }
    

    所以当你处理错误时,你可以去

    if(isset($_POST))
    {
        $FormErrors = new FormErrors("registration");
    
        if(strlen($_POST['username']) == 0)
        {
           $FormErrors->add('Username','Please enter a valid username');
        }
    
        //And for the rest of your checks
    }
    

    然后在你的html里面做

    foreach($FormErrors ->getErrors() as $name => $error)
    {
        echo sprintf("<p title=\"%s\">%s</p>",$name,$error);
    }
    

    应该可以,如果您想删除所有已知错误,请这样做

    $FormErrors = new FormErrors("registration");
    unset($FormErrors->handler,$FormErrors);
    

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 2016-10-29
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多