检查POST是否发生特定模式的对象名称 [英] Check whether a POST occured for an object name of particular pattern

查看:278
本文介绍了检查POST是否发生特定模式的对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的html标记(这是一个让你知道的基本结构,因为有些人会问我<< form> 标签在哪里。 )

 < input type =textname =set_val_1id =set_val_1value =1 /> 
< input type =textname =set_val_2id =set_val_2value =2/>
< input type =textname =max_risk_idid =max_risk_idvalue =5/>
< input type =submitvalue =Entername =submit_button/>

现在当表单被提交时,我想要有一个代码来检测是否有任何POST出现在格式 set_val _



基于我的问题的算法:

  if(isset($ _ POST ['something with the pattern(set_val_)']))
{
$ flag = 1;
$ val_string =;
}

if($ flag == 1)
{
$ max_id = $ _POST ['max_risk_id'];
for($ i = 1; $ i <= $ max_id; $ i ++)
{
if(isset($ _ POST ['set_val _'。$ i]))
{
$ val_string = $ val_string。 $ _ POST [ _ set_val $ I] 。
}
}
}

如何检查发布了一个特定的名称格式?



新的编辑解释了为什么我需要这个



我有多个多选,检查小提琴:



http:// jsfiddle。 net / p0b4j5o8 / 24 /

所以,为此,我以这种方式获取多重选择的数据

  if(isset($ _ POST ['max_risk_id']))
{
$ max_id = $ _POST ['max_risk_id'];
for($ i = 1; $ i <= $ max_id; $ i ++)
{
$ offer_combo ='';
if(isset($ _ POST ['risk_mitigator_offer _'。$ i])&& isset($ _ POST ['risk_weight _'。$ i]))
{
$ offer_risk = $ _POST [ 'risk_mitigator_offer _' $ I。];
$ risk_weight = $ _POST ['risk_weight _'。$ i];
print_r($ offer_risk);
foreach($ offer_risk as $ fr)
{
$ offer_combo = $ offer_combo。 $ fr。 ;
}
$ offer_combo = trim($ offer_combo,,);
$ mitigator_insert_query =INSERT INTO mt_risk_mitigator
(camp_id,risk_combination,risk_weight)
VALUES('。$ mysql ['camp_id']。'','$ offer_combo','$ risk_weight );
mysql_query($ mitigator_insert_query);




解决方案

使用 preg_grep 和键盘检查功能



pre $ 函数preg_grep_keys($ pattern,$ input,$ flags = 0){
return array_intersect_key($ input,array_flip(preg_grep($ pattern ,array_keys($ input),$ flags)));


if(count(preg_grep_keys('/ set_val_\\d + /',$ _POST))> 0)
{
$ flag = 1;
$ val_string =;
}


I have a following html markup (it's a basic structure to let you know, as some people will ask me where is the <form> tag. Consider it being there.)

<input type="text" name="set_val_1" id="set_val_1" value="1"/>
<input type="text" name="set_val_2" id="set_val_2" value="2"/>
<input type="text" name="max_risk_id" id="max_risk_id" value="5"/>
<input type="submit" value="Enter" name="submit_button"/>

Now when the form will be submitted I want to have a code to detect whether any POST appeared in the format "set_val_".

An algorithm based on my issue:

if(isset($_POST['something with the pattern (set_val_)']))
{
     $flag = 1;
     $val_string = "";
}

if($flag == 1)
{
    $max_id = $_POST['max_risk_id'];
    for($i = 1; $i<=$max_id; $i++)
    {
       if(isset($_POST['set_val_'.$i]))
        {
            $val_string = $val_string. $_POST['set_val_'.$i].",";
        }
    }
}

How can I check whether a post occurred of a particular name format?

New edit explaining why I need this

I have multiple multiselect, check the fiddle:

http://jsfiddle.net/p0b4j5o8/24/

So for that, I am having the fetching data of the multi-selects in this fashion

if(isset($_POST['max_risk_id']))
                {
                    $max_id = $_POST['max_risk_id'];
                    for($i = 1; $i<=$max_id; $i++)
                    {
                        $offer_combo = '';
                        if(isset($_POST['risk_mitigator_offer_'.$i]) && isset($_POST['risk_weight_'.$i]))
                        {
                            $offer_risk = $_POST['risk_mitigator_offer_'.$i];
                            $risk_weight = $_POST['risk_weight_'.$i];
                            print_r($offer_risk);
                            foreach($offer_risk as $fr)
                            {
                                $offer_combo = $offer_combo . $fr . ",";
                            }
                            $offer_combo = trim($offer_combo,",");
                            $mitigator_insert_query = "INSERT INTO mt_risk_mitigator 
                                                        (camp_id , risk_combination, risk_weight) 
                                                        VALUES ('".$mysql['camp_id']."','$offer_combo','$risk_weight')";
                            mysql_query($mitigator_insert_query);
                        }
                    }
                }

解决方案

Use preg_grep with keys checking function

function preg_grep_keys($pattern, $input, $flags = 0) {
    return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags)));
}

if(count(preg_grep_keys('/set_val_\d+/', $_POST)) > 0)
{
     $flag = 1;
     $val_string = "";
}

这篇关于检查POST是否发生特定模式的对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆