簡體   English   中英

PHP聯系表格,我做錯了嗎?

[英]PHP contact form, am I doing it wrong?

我正在學習PHP,並且試圖編寫一個簡單的電子郵件腳本。 我有一個函數(checkEmpty)來檢查是否填寫了所有表格,以及電子郵件地址是否有效(isEmailValid)。 我不確定如何返回真實的checkEmpty函數。 這是我的代碼:

單擊提交按鈕時:

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

//INSERT FORM VALUES INTO AN ARRAY
$field = array ('name' => $_POST['name'], 'email' => $_POST['email'], 'message' => $_POST['message']);

//CONVERT ARRAY KEYS TO VARIABLE NAMES
extract ($field);
checkEmpty($name, $email, $message);



function checkEmpty($name, $email, $message) {  
    global $name_error;
    global $mail_error;
    global $message_error;

    //CHECK IF NAME FIELD IS EMPTY
    if (isset($name) === true && empty($name) === true) {
    $name_error = "<span class='error_text'>* Please enter your name</span>";
    }

//CHECK IF EMAIL IS EMPTY
if (isset($email) === true && empty($email) === true) {
    $mail_error = "<span class='error_text'>* Please enter your email address</span>";
    //AND IF IT ISN'T EMPTY CHECK IF IT IS A VALID ONE
    } 
    elseif (!isValidEmail($email)) {
        $mail_error = "<span class='error_text'> * Please enter a valid email</span>"; 
    }

    //CHECK IF MESSAGE IS EMPTY
    if (isset($message) === true && empty($message) === true) {
    $message_error = "<span class='error_text'>* Please enter your message</span>";
    }
} 

// This function tests whether the email address is valid  
function isValidEmail($email){
    $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
    if (eregi($pattern, $email))
        {
            return true;
        } else 
        {
            return false;
        }   
    }

我知道我不應該在函數中使用全局變量,我不知道其他方法。 錯誤消息顯示在每個表單元素旁邊。

首先,您應該重新考慮自己的邏輯,以避免全局變量。

無論哪種方式,都需要在函數頂部創建一個變量$ success並將其設置為true。 如果任何if語句失敗,請將其設置為false。 然后在函數底部返回$ success。 例:

function checkExample($txt) {
    $success = true;

    if (isset($txt) === true && empty($txt) === true) {
        $error = "<span class='error_text'>* Please enter your example text</span>";
        $success = false;
    }

    return $success;
}

我不確定這是您想要的東西,還是我希望通過功能外部訪問$ mail_error,$ message_error和$ name_error。 如果是這樣,您需要的是這樣的:

function checkEmpty($name, $email, $message) {  
    $results = false;

    //CHECK IF NAME FIELD IS EMPTY
    if (isset($name) === true && empty($name) === true) {
      $results['name_error'] = "<span class='error_text'>* Please enter your name</span>";
    }

    //CHECK IF EMAIL IS EMPTY
    if (isset($email) === true && empty($email) === true) {
      $results['mail_error'] = "<span class='error_text'>* Please enter your email address</span>";
    //AND IF IT ISN'T EMPTY CHECK IF IT IS A VALID ONE
    } 
    elseif (!isValidEmail($email)) {
        $results['mail_error'] = "<span class='error_text'> * Please enter a valid email</span>"; 
    }

    //CHECK IF MESSAGE IS EMPTY
    if (isset($message) === true && empty($message) === true) {
      $results['message_error'] = "<span class='error_text'>* Please enter your message</span>";
    }

    return $results;
} 
$errors = checkEmpty($name, $email, $message);

現在您可以測試錯誤

if($errors){
    extract ($errors); // or simply extract variables from array to be used next to form inputs
} else {
    // there are no errors, do other thing if needed...
}

首先,使用全局是一種罪過。 您正在污染全局名稱空間,這是個壞主意,除了很少的即席腳本和舊代碼之外。

其次,您正在濫用isset-出於兩個原因:a)在給定的上下文中,您將變量$ name傳遞給函數,因此始終將其設置為b)empty檢查是否設置了變量

第三,您應該將驗證與生成html分開。

第四,您可以使用filter_var而不是正則表達式來測試郵件是否有效。

最后,您的代碼可能如下所示:

<?php

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

$fields = array ('name' => $_POST['name'], 'email' => $_POST['email'], 'message' =>     $_POST['message']);

//CONVERT ARRAY KEYS TO VARIABLE NAMES
extract ($fields);  

$errors = validateFields($name, $email, $message);

if (!empty($errors)){

    # error 

    foreach ($errors as $error){

        print "<p class='error'>$error</p>";

    }

} else {

    # all ok, do your stuff

} // if

} // if

function validateFields($name, $email, $post){

    $errors = array();

        if (empty($name)){$errors[] = "Name can't be empty";}
        if (empty($email)){$errors[] = "Email can't be empty";}
        if (empty($post)){$errors[] = "Post can't be empty";}

        if (!empty($email) && !filter_var($email,FILTER_VALIDATE_EMAIL)){$errors[] = "Invalid email";}
        if (!empty($post) && strlen($post)<10){$errors[] = "Post too short (minimum 10 characters)";}

    # and so on...

    return $errors;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM