2010-07-16 69 views
0

我有一个HTML表单,它接收输入的数据并通过mail()函数发送它。我也有一些验证技术来验证输入,并且我创建了一个数组变量$ errors来记录所有的错误;例如,使用PHP来请求错误信息并发布,如果错误有什么

如果名称留空,$ errors [] =“Name empty”; 如果电子邮件为空,$ errors [] =“电子邮件为空”;

等..

我可以使用以下方法来报告错误:

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 

不过,我要的是以下几点。我想将页面重定向回原来的表单,用于输入信息(我知道确切的链接位置,所以我可以使用标题()或甚至可以使用<meta=http-equiv=refresh>将我带回表单页面

而且,在表格上,我希望能够张贴在某些格形式上面的错误(称之为DIV =错误)

我将能够做到以下几点?

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

非常感谢!

Amit

+0

哦,最后一两件事,这种形式位于WordPress的页面上。我怎样才能将PHP输入到wordpress页面?它以段落形式输入它,即使我正在使用带有raw_html插件的HTML编辑器? – Amit 2010-07-16 14:34:56

回答

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 ... --> 
0

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

+0

重定向用户有助于避免刷新页面时再次提交数据的问题。另外,您可以将表单显示分为控制器A和表单分析到控制器B.如果用户在注销后提交表单(用户重定向登录:用户登录,返回表单,表单填充因为数据在$ _SESSION中)。 – 2010-07-16 14:38:26

1

做到这一点,最简单的方法是:

// 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) ... ; 
+0

这看起来不错。我在想,下面的头文件是做什么的? header('HTTP/1.1 303 See Other'); 我有过这样的时候,header()实际上并没有重定向我,是否因为我错过了那一行? – Amit 2010-07-16 14:52:08

+0

默认情况下,header('Location:...')使用302 HTTP状态,在HTTP的某些解释下,浏览器会将相同的请求提交到新位置(在这种情况下,具有相同的POST变量)。使用303会导致它执行GET,丢弃任何POST参数。 – 2010-07-16 14:54:19

+0

@Nillocet:尽管这样做会很好,Butthis会让$ _SESSION变得更重,所以它不是一个好习惯。 – Sadat 2010-07-16 15:08:27

0

确保您的会议在你的应用程序的顶部开始

包括这个基本类

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);