Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

php - Check whether a POST occured for an object name of particular pattern

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);
                        }
                    }
                }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 = "";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...