PHP session array and input validation

时间:2016-05-11 11:32:31

标签: php arrays forms session

Currently I am sending error messages and storing the value of my input fields in sessions.

Form example

<label class="errormsg"><?php echo $_SESSION['msgProductPrice']; unset($_SESSION['msgProductPrice']); ?></label>

<input type="text" name="product_price" value="<?php echo $_SESSION['val_ProductPrice']; unset($_SESSION['val_ProductPrice']); ?>" />

PHP

$Price = $_POST['product_price'];
$errorcount = 0;

if(empty($Price) === true){
    $PriceErrormsg = "empty";
    $errorcount++;
}

if($errorcount === 0) {
   // success
} else {
   $_SESSION['val_ProductPrice'] = $Price;
   $_SESSION['msgProductPrice'] = $PriceErrormsg;
}

This works perfectly with one of a kind input field. If I try with multiple input fields with the same name, it doesn't work.

Form example

<label class="errormsg"><?php echo $_SESSION['msgProductAmount']; unset($_SESSION['msgProductAmount']); ?></label>
<input type="text" name="product_amount[]" value="<?php echo $_SESSION['val_ProductAmount']; unset($_SESSION['val_ProductAmount']); ?>" />

<label class="errormsg"><?php echo $_SESSION['msgProductAmount']; unset($_SESSION['msgProductAmount']); ?></label>
<input type="text" name="product_amount[]" value="<?php echo $_SESSION['val_ProductAmount']; unset($_SESSION['val_ProductAmount']); ?>" />

This is where I'm unsure on how to validate all the input fields, how to keep the value in each input field when you hit submit and how to send an errormsg about each field?

PHP

$Amount= $_POST['product_amount'];
$errorcount = 0;

if(empty($Amount) === true){
    $AmountErrormsg = "empty";
    $errorcount++;
}

if($errorcount === 0) {
   // success
} else {
   $_SESSION['val_ProductAmount'] = $Amount;
   $_SESSION['msgProductAmount'] = $AmountErrormsg;
}

2 个答案:

答案 0 :(得分:1)

如果我了解您的问题,则会提交多个产品数量,并且您希望单独验证每个产品数量并在相应的文本框旁边显示错误消息?

因为您正在接收值数组,所以需要创建相应的错误消息数组。

我已经有一段时间了,因为我已经完成了任何PHP,所以这可能不是100%正确,但我认为你需要这些内容......

$AmountErrorMessage = Array();
foreach ($Amount as $key => $value) {
    if (empty($value)) {
        $AmountErrorMessage[$key] = 'empty';
    }
}

if ($AmountErrorMessage->count() > 0) {
    // success
} else {
    $_SESSION['val_ProductAmount'] = $Amount;
    $_SESSION['msgProductAmount'] = $AmountErrorMessage;
}

然后,您还需要遍历数组以生成表单的HTML,为每个提交的值创建标签和输入框。

答案 1 :(得分:0)

此代码可帮助您按照自己的意愿进行操作..

<?php
    session_start();
    ?>    
    <html>
        <head>
            <title></title>    
        <style>
        .errormsg{
         color:red; 
        }
        </style>  
        </head>
        <body>    
        <?php
        if(isset($_POST['product_amount']))
        {
          $errorcount = 0;
          for($i=0;$i<count($_POST['product_amount']);$i++){
            $Amount[$i] = $_POST['product_amount'][$i];  

            if(empty($Amount[$i]) === true){
                $_SESSION['msgProductAmount'][$i] = "empty";
                $errorcount++;
            }
            else
              $_SESSION['val_ProductAmount'][$i] = $Amount[$i];
          }
          if($errorcount === 0) {
             unset($_SESSION['msgProductAmount']); 
             echo "success";
          } 
        }
        ?>
        <form action="" method="POST">
        <?php
        $cnt = 10;
        for($i=0;$i<$cnt;$i++){
        ?>
        <input type="text" name="product_amount[<?=$i?>]" value="<?php echo isset($_SESSION['val_ProductAmount'][$i]) ? $_SESSION['val_ProductAmount'][$i] : '';?>" />
        <label class="errormsg"><?php echo $res = isset($_SESSION['msgProductAmount'][$i]) ? $_SESSION['msgProductAmount'][$i] : '' ; ?></label>
        <br/>
        <?php
        }
        ?>
        <input type="submit" name="submit" value="submit" />
        </form>
        </body>
        </html>
        <?php
        unset($_SESSION['msgProductAmount'],$_SESSION['val_ProductAmount']);
        ?>