2012-10-02 47 views
0

所以我做了一個網頁,它有一個表單,並找到一些相對直接的代碼來獲取表單的內容,檢查它是否有錯誤或不完整的部分,並返回錯誤或發送它。不幸的是,它在新頁面中返回錯誤,而不是在表單頁面本身內,這是我想要的。在網頁中添加文本

HTML表單:

<form name="contactform" method="post" action="send_form.php"> 
     <table width="450px"> 
      <tr> 
       <td valign="top"> 
        <label for="first_name">First Name &#42</label> 
       </td> 
       <td valign="top"> 
        <input type="text" name="first_name" maxlength="50" size="30"> 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        <label for="last_name">Last Name &#42</label> 
       </td> 
       <td valign="top"> 
        <input type="text" name="last_name" maxlength="50" size="30"> 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        <label for="email">Email Address &#42</label> 
       </td> 
       <td valign="top"> 
        <input type="text" name="email" maxlength="80" size="30"> 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        <label for="telephone">Telephone Number</label> 
       </td> 
       <td valign="top"> 
        <input type="text" name="telephone" maxlength="30" size="30"> 
       </td> 
      </tr> 
      <tr> 
       <td valign="top"> 
        <label for="comments">Comments &#42</label> 
       </td> 
       <td valign="top"> 
        <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="2" style="text-align:center"> 
        <h6>An astrisk (&#42) denotes a required field.</h6> 
        <input type="submit" value="Submit" /> 
       </td> 
      </tr> 
     </table> 
    </form> 

的PHP代碼:

if(isset($_POST['email'])) { 

$email_to = "[email protected]"; 
$email_subject = "Comments"; 


function died($error) { 
    //This is where I think the code that prints to the same webpage should be rather than putting on a new page, which is what I does now 
    echo "There were error(s) found with the form you submitted. Please review these errors and resubmit."; 
    echo "These errors appear below.<br /><br />"; 
    echo $error."<br /><br />"; 
    echo "Please go back and fix these errors.<br /><br />"; 
    die(); 
} 

// validation expected data exists 
if(!isset($_POST['first_name']) || 
    !isset($_POST['last_name']) || 
    !isset($_POST['email']) || 
    !isset($_POST['telephone']) || 
    !isset($_POST['comments'])) { 
    died('We are sorry, but there appears to be a problem with the form you submitted.');  
} 

$first_name = $_POST['first_name']; // required 
$last_name = $_POST['last_name']; // required 
$email_from = $_POST['email']; // required 
$telephone = $_POST['telephone']; // not required 
$comments = $_POST['comments']; // required 

$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
$error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$first_name)) { 
$error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 
if(!preg_match($string_exp,$last_name)) { 
$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
} 
if(strlen($comments) < 2) { 
$error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
} 
if(strlen($error_message) > 0) { 
died($error_message); 
} 
$email_message = "Form details below.\n\n"; 

function clean_string($string) { 
    $bad = array("content-type","bcc:","to:","cc:","href"); 
    return str_replace($bad,"",$string); 
} 

$email_message .= "First Name: ".clean_string($first_name)."\n"; 
$email_message .= "Last Name: ".clean_string($last_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 


// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 

<!-- email success html --> 
<h4>Thank you for contacting us. We will be in touch with you very soon.</h4> 
<h4><a class="submitSuccess" href="index.php">Return to Index</a></h4> 

} 

我基本上希望它打印的文本說,形式沒有得到正確填寫上面的一行文字。現在它會創建一個包含錯誤的新網頁。

旁註: 應該有幾個php括號,但代碼沒有正確顯示與他們在一起,所以我把他們拿出來。儘管如此,您仍然可以看到代碼的概念。

+0

die將輸出消息並終止當前腳本 – GBD

回答

2

看你的代碼,PHP頁面的基本行爲是檢查是否有任何驗證錯誤。如果是,則顯示錯誤消息,否則發送電子郵件。

你想要做的是顯示帶有驗證錯誤的原始頁面。

通常,至少在幾乎所有我知道的框架中,這是通過使用重定向完成的。 基本上,如果沒有錯誤,我們將用戶重定向到成功頁面。

如果有錯誤,我們會將錯誤信息+用戶數據放在會話變量中並重定向到原始表單。

基本的代碼結構是這樣的:

驗證腳本:

if (validate_form($_POST)) 
{ 
    /* 
     Form validation succeeded, 
     Do whatever processing you want (Like sending an email) 
    */  
    header('location: success.php'); // redirect to the success page 
} 
else 
{ 
    /* 
     Form validation failed   
    */ 
    session_start(); 
    $error = ...; 
    $form_data = array('error' => $error, 'username' => $_POST['username'], ...); 
    $SESSION['form_data'] = $form_data; 
    header('location: form.php'); 

} 

形式腳本:

<?php 
    session_start(); 
    if(isset($SESSION['form_data'])) 
    { 
     $username = $SESSION['form_data']['username']; 
     $errors = $SESSION['form_data']['error']; 
    } 
    else 
    { 
     $username = ''; 
     $errors = '';   
    } 

?> 

<form name="contactform" method="post" action="send_form.php"> 
     <input type="text" name="username" value=<?php $username ?> >  
     <label for="errors"><?php $errors ?></label> 
</form> 

PS:此代碼是一個簡單的示範,顯然不是100%正確的。你必須添加更多的異常處理,更多的安全檢查...但你明白了。

我建議您嘗試使用輕量級MVC框架來了解如何以正確的方式完成此操作。你甚至可以看看這些框架的源代碼。

0

當你的表單數據沒有通過你的驗證服務器端時,你可以調用函數death()並在最後使用die()。請檢查PHP幫助以確保die()能夠滿足您的需求。原因是告訴PHP停止解析PHP並將頁面AS-IS發送給客戶端。所以你的表單永遠不會出現。

Personnaly:我使用的芯片功能用於調試目的只

0

經雲:

<form name="contactform" method="post" action="send_form.php"> 

你告訴HTML表單走在Web瀏覽器中send_form.php。 然後,在send_form.php中,完成邏輯以查看錶單的內容是否正確,以便在該新頁面上顯示回顯。

您將不得不在原始HTML頁面中使用一些腳本(javascript/php)來檢查表單是否填寫正確。

看到這樣的事情:http://www.thesitewizard.com/archive/validation.shtml