2010-07-16 27 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);