簡體   English   中英

PHP表單驗證問題-驗證大型輸入字段集

[英]PHP Form Validation Issue - Validate Large Set of Input Fields

我的HTML表單中有20個“集合”的輸入字段。

這是一個品嘗者:

<input id="a1height" />
<input id="a1width" />
<input id="a1length" />
<input id="a1weight" />

<input id="a2height" />
<input id="a2width" />
<input id="a2length" />
<input id="a2weight" />
...and so on

現在,我需要一種方法:

a)將所有值存儲在一個變量中,每組之間的高度,寬度,長度和重量以及\\ n之間用豎線(|)之間的管道(|)b)如果一組中的一個或多個字段不完整,則變量$ errors設置為真正。

我對如何實現這一目標感到有些茫然。 循環永遠都不好用:'(

有人可以解釋如何做嗎?

非常感謝!!

這是一個使用$errors數組存儲所有錯誤的解決方案,並使用$all字符串包含所需的輸出。 要進行檢查,請輸入var_dump( $all); 在腳本末尾。

$errors = array();
$all = '';

// Loop over the values 1 through 20
foreach( range( 1, 20) as $i)
{
    // Create an array that stores all of the values for the current number
    $values = array( 
        'a' . $i . 'height' => $_POST['a' . $i . 'height'], 
        'a' . $i . 'width' => $_POST['a' . $i . 'width'], 
        'a' . $i . 'length' => $_POST['a' . $i . 'length'], 
        'a' . $i . 'weight' => $_POST['a' . $i . 'weight']
    );

    // Make sure at least one submitted value is valid, if not, skip these entirely
    if( count( array_filter( array_map( 'is_numeric', $values))))
    {
         // This basically checks if there's at least one numeric entry for the current $i
         continue; // Skip
    }

    // Validate every value
    foreach( $values as $key => $value)
    {
        if( empty( $value))
        {
            $errors[] = "Value $key is not set";
        }
        // You can add more validation in here, such as:
        if( !is_numeric( $value))
        {
            $errors[] = "Value $key contains an invalid value '$value'";
        }
    }

    // Join all of the values together to produce the desired output
    $all .= implode( '|', $values) . "\n";
}

要很好地格式化錯誤,請嘗試如下操作:

// If there are errors
if( count( $errors) > 0)
{
    echo '<div class="errors">';

    // If there is only one, just print the only message
    if( count( $errors) == 1)
    {
        echo $errors[0];
    }
    // Otherwise format them into an unordered list
    else
    {
        echo '<ul>';
        echo '<li>' . implode( '</li><li>', $errors) . '</li>';
        echo '</ul>';
    }

    echo '</div>';
}

首先應該為您的輸入創建NAME屬性,因為這些屬性將在_POST數組中用於指定您的值。

<input id="a1height" name="a1height" />
<input id="a1width" name="a1width" />
<input id="a1length" name="a1length" />
<input id="a1weight" name="a1weight" />

<input id="a2height" name="a2height" />
<input id="a2width" name="a2width" />
<input id="a2length" name="a2length" />
<input id="a2weight" name="a2weight" />

然后,在提交后,遍歷post數組以創建管道連接的變量

$errors = false;
$string = "";
$current_prefix = '';
foreach($_POST as $key=>$posted_value){
   if(trim($posted_value)==""){ //if the value is empty or just spaces
      $errors = TRUE;
   }
   //find out if we need to add a new line
   $number = preg_replace ('/[^\d]/', '', $key) //get the numbers of the name only
   if ($current_prefix != $number){
       $string .= "\n";
   } else {
      $string .= '|';
   }
   $string .= $posted_value;
}
  1. 創建一個數組來存儲您的值。
  2. 遍歷您的輸入字段,從字段類型(即“ height”)中解析前綴(即“ a1”)
  3. 像這樣將值插入數組:

    $your_array['a1']['height'] = $_POST['a1height'];

  4. 記住要驗證並清理您的輸入。

您可以更改您的html,以便發布的字段采用格式正確的數組:

<input name="a1[height]" />
<input name="a1[width]" />
<input name="a1[length]" />
<input name="a1[weight]" />

<input name="a2[height]" />
<input name="a2[width]" />
<input name="a2[length]" />
<input name="a2[weight]" />

這樣,您將獲得一個易於使用的數組。 您可以訪問$_POST['a1']['height']$_POST['a1']['length']$_POST['a2']['height']

暫無
暫無

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

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