dongxingchang9345 2016-05-11 11:32
浏览 31
已采纳

PHP会话数组和输入验证

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条回答 默认 最新

  • dspym82000 2016-05-11 11:57
    关注

    If I understand your problem, multiple product amounts are being submitted, and you want to validate each one individually and display the error message next to the appropriate textbox?

    Because you are receiving an array of values, you need to create a corresponding array of error messages.

    It's a while since I've done any PHP, so this might not be 100% correct, but I think you need something along these lines...

    $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;
    }
    

    You would then also need to iterate through the array in order to generate the HTML for your form, creating a label and input box for each value submitted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法