2016-04-25 40 views
0

我仍然對PHP很陌生並且仍在學習語法。整合驗證是一個壞主意嗎?目前,我正在驗證每個單獨的字段,因爲它正在提取。像這樣;PHP整合驗證

if ($_SERVER["REQUEST_METHOD"] == "POST") { 

if (empty($_POST["fname"])) { 
    $fnameErr = "First name is required"; 
    ++$inc; 
    } else { 
    $fnameField = test_input($_POST["fname"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$fnameField)) { 
     $fnameErr = "First Name: error - (Text & spaces only.)"; 
     ++$inc; 
     } 
    } 

if (empty($_POST["lname"])) { 
    $lnameErr = "Last name is required"; 
    ++$inc; 
    } else { 
    $lnameField = test_input($_POST["lname"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$lnameField)) { 
     $lnameErr = "Last Name: error - (Text & spaces only.)"; 
     ++$inc; 
     } 
    } 

    if (empty($_POST["company"])) { 
    $companyErr = "Company name is required"; 
    ++$inc; 
    } else { 
    $companyField = test_input($_POST["company"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$companyField)) { 
     $companyErr = "Company: error - (Text & spaces only.)"; 
     ++$inc; 
     } 
    } 

基本上,值得把這三個參數合併成一個嗎?如果是這樣,我會怎麼做呢?

編輯:更新的問題給出完整的代碼和它的變量。

會是這樣的似是而非?在我不確定達到目標的最佳方式後,我知道我的基本概念。

PHP - 根據@nerdlyist的建議進行更改。

<?php 

// session start. 
    session_start(); 

// set post data as array. 
    $_SESSION['post-data'] = $_POST; 

// post data array. (for note purposes to give an idea of what is in the array.) 
// $_SESSION['post-data']['fname']; 
// $_SESSION['post-data']['lname']; 
// $_SESSION['post-data']['com']; 
// $_SESSION['post-data']['ttl']; 
// $_SESSION['post-data']['ema']; 
// $_SESSION['post-data']['add1']; 
// $_SESSION['post-data']['add2']; 
// $_SESSION['post-data']['cou']; 
// $_SESSION['post-data']['tel']; 
// $_SESSION['post-data']['day']; 
// $_SESSION['post-data']['act']; 
// $_SESSION['post-data']['chk']; // << these are checkboxes. 
// $_SESSION['post-data']['rdo']; // << these are radios. 

// subject & account. 
    $emailSub = 'Drupa 2016 - Booking Form Actioned'; 
    $emailAcc = '[email protected]'; 

// data validation. 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    $names = array(
    "fname" => $_POST['fname'], // first name field. 
    "lname" => $_POST['lname'], // last name field. 
    "com" => $_POST['com'], // company name field. 
    "ttl" => $_POST['ttl'], // title field. 
    "ema" => $_POST['ema'], // email field. 
    "add1" => $_POST['add1'], // address line 1 field. 
    "add2" => $_POST['add2'], // address line 2 field. 
    "cou" => $_POST['cou'], // country field. 
    "tel" => $_POST['tel'] // tel field. 
); 

    $errors = array(); 

    foreach($names as $name => $value){ 
     if (empty($value)) { 
     $errors[] = $name."_blank"; 
     } else { 
      // fetch data from cleaner. 
      $fnameField = test_input($_POST["fname"]); 
      $lnameField = test_input($_POST["lname"]); 
      $comField = test_input($_POST["com"]); 
      $ttlField = test_input($_POST["ttl"]); 
      $couField = test_input($_POST["cou"]); 
      // check if name only contains letters and whitespace 
      if (!preg_match("/^[a-zA-Z ]*$/",$value)) { 
       //you can only have one or the other. 
       $errors[] = $name."_clean"; 
      } 
     } 
    } 

    // determining what submit or re-display. 
    if(empty($errors)){ 
     echo "Clean form to submit"; 
    } else { 
     echo "Rebuild the form and parse errors: "; 
     print_r($errors); 
    } 
} 

// for cleaning the data. 
    function test_input($data) { 

    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 

    return $data; 
    } 

    // checkbox array. 
    $selectedProjects = 'None'; 
    if(isset($_POST['chk']) && is_array($_POST['chk']) && count($_POST['chk']) > 0){ 
     $selectedProjects = implode(', ', $_POST['chk']); 
    } 

    // radio array. 
    $selectedTime = 'Afternoon'; 
    if(isset($_POST['rdo']) && is_array($_POST['rdo']) && count($_POST['rdo']) > 0){ 
     $selectedTime = implode(', ', $_POST['rdo']); 
    } 

    // mail body. 
    $body = <<<EOD 
<h3>Booking Request/$date</h3> 
<hr><br> 
Last Name: $lnameField <br> 
First Name: $fnameField <br> 
Company: $companyField <br> 
Title: $titleField <br> 
Email: $emailField <br> 
Acitivity: $actField <br> 
<br> 
<h3>Contact Info</h3> 
<hr><br> 
Add Line 1: $add1Field <br> 
Add Line 2: $add2Field <br> 
Country: $countryField <br> 
Telephone: $telField <br> 
<br> 
Requested Booking day: $daySelect <br> 
Requested Booking Time: $selectedTime <br> 
<br> 
Interested in: $selectedProjects <br> 
submitted: <b>$date</b> at <b>$time</b>. 
EOD; 

// form submission check. 
    if isset($_POST['btn-sub'])) { 

    // code executed on submit 
     $headers = "MIME-Version: 1.0\n" ; 
     $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
     $headers .= "X-Priority: 1 (Highest)\n"; 
     $headers .= "X-MSMail-Priority: High\n"; 
     $headers .= "Importance: High\n"; 
     $headers = "From: $emailField\r\n"; 

     $success = mail($emailAcc, $emailSub, $body, $headers); 

    } else { 
    // code executed on first request 

    // set date & time. 
     $date = date ("l, F jS, Y"); 
     $time = date ("h:i A"); 

    // define variables and set to empty values. 
     $err = ""; 
     $fnameField = $lnameField = $companyField = $titleField = $emailField = $add1Field = $add2Field = $countryField = $telField = $daySelect = $actSelect = $chk = $rdo= ""; 
    } 

    // redirect & exit. 
    header('Location: prox.php'); 
    exit(); 

?> 
+1

它將堅持DRY的原則,使其成爲一個函數,然後只是傳入你正在驗證的名稱。你需要弄清楚錯誤信息,但這應該是微不足道的。 – nerdlyist

+0

你的意思是像傳遞數據到數組並傳遞參數而不是單個目標的數組? – Beaniie

+0

強烈建議使用POO來不重複這種情況下的代碼。 –

回答

1

這是讓你開始的東西。它會給錯誤添加一個錯誤。不知道你是如何建立窗體,但循環錯誤,如果錯誤是_blank字段是必需的,如果它是_clean有你不喜歡的字符。

$names = array(
    "fname" => $_POST['fname'], 
    "lname" => $_POST['lname'], 
    "company" => $_POST['company'] 
); 

$errors = array(); 
$inc = 0; //Not sure what this was for. 
foreach($names as $name => $value){ 
    if (empty($value)) { 
    $errors[] = $name."_blank"; 
    ++$inc; 
    } else { 
     //Not sure what this does 
     //$fnameField = test_input($_POST["fname"]); 
     // check if name only contains letters and whitespace 
     if (!preg_match("/^[a-zA-Z ]*$/",$value)) { 
      //you can only have one or the other. 
      $errors[] = $name."_clean"; 
      ++$inc; 
     } 
    } 
} 

//This is where you can determine to submit or re-display. 
if(empty($errors)){ 
    echo "Clean form to submit"; 
} else { 
    echo "Rebuild the form and parse errors: "; 
    print_r($errors); 
} 
+0

我已更新您的問題,以幫助您瞭解「++ $ inc」和「test_input()」的用途。此外,請查看更新後的問題以獲取完整的代碼概述,並嘗試實現您的答案。 – Beaniie