2016-01-22 64 views
0

我知道PHP表單驗證經常在這裏觸及,但我是一個完整的PHP新手,我只是在某種形式的PHP表單腳本中發現我發現here,所以我我不知道如何在這裏處理我需要的兩件事。需要勾選複選框的PHP表單提交

我希望我的表單要求條件框&將在提交之前進行檢查(並且如果未選中此框,以此腳本處理錯誤消息的方式返回錯誤消息)。

我還希望條款&條件框被檢查通過電子郵件發送給我的其餘表格數據。

,我試過了HTML的顯着位是:

<td><input type="checkbox" name="terms-and-conditions" value="terms-and-conditions" /><label for="terms-and-conditions">I have read the <a href="terms.html">Terms & Conditions</a> *</label></td> 

PHP的形式我已經試過改變是:

<?php 

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



    // EDIT THE 2 LINES BELOW AS REQUIRED 

    $email_to = "[email protected]"; 

    $email_subject = "Order Form"; 





    function died($error) { 

     // your error code can go here 

     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 

     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['URL']) || 

     !isset($_POST['date']) || 

     !isset($_POST['comments']) || 

     !isset($_POST['terms-and-conditions'])) { 

     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 

    $URL = $_POST['URL']; // required 

    $date = $_POST['date']; // not required 

    $comments = $_POST['comments']; // not required 

    $terms-and-conditions = $_POST['terms-and-conditions']; // 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($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 .= "URL: ".clean_string($URL)."\n"; 

    $email_message .= "Date: ".clean_string($date)."\n"; 

    $email_message .= "Comments: ".clean_string($comments)."\n"; 

    $email_message .= "Terms And Conditions: ".clean_string($terms-and-conditions)."\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); 

?> 



<!-- include your own success html here --> 

Thank you for submitting the order form. 


<?php 

} 

?> 

隨着我已經添加了什麼,該表單現在會拋出500內部服務器錯誤。我能夠成功地添加和刪除這個php表單中的輸入字段,但是我不知道如何要求它在提交之前填充一個複選框(如果沒有填充它,它會返回錯誤信息)並通過電子郵件發送給我,其餘的數據已填寫。

+0

難道您不想使用JavaScript檢查T&C框是否已選中,或者您是否只對純服務器端驗證感興趣?在附註中,使用標籤的for屬性時需要相應的ID屬性。所以用'

+0

@ j08691我真的不介意什麼,只要它能夠工作,我就能實現它!謝謝;將看着你的標籤筆記。 – VAnton

回答

1

如果您想驗證複選框是否打勾(選中)之前表單發送到服務器,您將需要一些JavaScript。 一個很(非常!)原油檢查會是什麼樣子

<form onsubmit="return this.elements['terms-and-conditions'].checked;">

這將(默默的,所以沒有反饋),防止表單提交如果「條款和條件」複選框未處於選中狀態。如上所述,這是一種非常粗糙的預防,根本不可用。 Please see this topic for more elaborate checks and solutions

至於爲什麼服務器引發500 Internal Server Error: 你不能有一個變量在他們-字符,所以$terms-and-conditions提高了解析錯誤。 請考慮使用下劃線代替,例如$terms_and_conditions$termsAndConditions

+0

嘗試將連字符更改爲下劃線,但仍會引發錯誤。我會瀏覽你鏈接的主題。謝謝。 – VAnton