dongwusang0314 2015-12-31 17:35
浏览 82

验证数组

I would like to validate my form and set the given values back to the input fields.

This is what var_dump($_GET) returns when I send the form:

array(10) { 
    ["ich_vorname"]=> string(5) "sdfdf" 
    ["ich_name"]=> string(0) "" 
    ["ich_strasse_nr"]=> string(0) "" 
    ["ich_plz_ort"]=> string(0) "" 
    ["ich_email"]=> string(0) "" 
    ["ich_konto_nr"]=> string(0) "" 
    ["friend_vorname"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_name"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_strasse_nr"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_plz_ort"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
}

As you see there are some strings and some arrays. For the strings I am using the following code to find out if the value is set. If it is set, so I can use something like value="<?= $_SESSION['ich_vorname'] ?>".

$_SESSION['ich_vorname'] = (isset($_GET['ich_vorname']) && !empty($_GET['ich_vorname'])) ? $_GET['ich_vorname'] : 'error';
$_SESSION['ich_name'] = (isset($_GET['ich_name']) && !empty($_GET['ich_name'])) ? $_GET['ich_name'] : 'error';
$_SESSION['ich_strasse_nr'] = (isset($_GET['ich_strasse_nr']) && !empty($_GET['ich_strasse_nr'])) ? $_GET['ich_strasse_nr'] : 'error';
$_SESSION['ich_plz_ort'] = (isset($_GET['ich_plz_ort']) && !empty($_GET['ich_plz_ort'])) ? $_GET['ich_plz_ort'] : 'error';
$_SESSION['ich_email'] = (isset($_GET['ich_email']) && !empty($_GET['ich_email'])) ? $_GET['ich_email'] : 'error';
$_SESSION['ich_konto_nr'] = (isset($_GET['ich_konto_nr']) && !empty($_GET['ich_konto_nr'])) ? $_GET['ich_konto_nr'] : 'error';

The strings are not a problem but the arrays are! How can I set the value for the array fields when the fields are getting generated dynamically?

  • 写回答

3条回答 默认 最新

  • dongpa5207 2015-12-31 17:52
    关注

    You can do something like this to validate the form inputs and store them in $_SESSION superglobal.

    foreach($_GET as $key => $value){
        if(is_array($value)){
            $tmp_arr = array();
            foreach($value as $v){
                $v = trim($v);
                if(!empty($v)){
                    $tmp_arr[] = $v;
                }else{
                    $tmp_arr[] = "error";
                }
            }
            $_SESSION[$key] = $tmp_arr;
        }else{
            $value = trim($value);
            if(!empty($value)){
                $_SESSION[$key] = $value;
            }else{
                $_SESSION[$key] = "error";
            }
        }
    }
    
    var_dump($_SESSION);  // to display $_SESSION contents
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么