-1

I have a following html markup (it's a basic structure to let you know, as some people will ask me where is the 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_". I hope I can make you understand what I am actually asking.

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?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Saswat
  • 12,320
  • 16
  • 77
  • 156
  • 2
    possible duplicate of [Check whether a POST occured for an object name of particular patter](http://stackoverflow.com/questions/30502817/check-whether-a-post-occured-for-an-object-name-of-particular-patter) – Epodax May 28 '15 at 10:18
  • @Epodax, so what? Nobody is answering that question. SO I reposted – Saswat May 28 '15 at 10:18
  • see this answer, may be useful you... http://stackoverflow.com/a/1596343/926333 – Girish May 28 '15 at 10:22
  • 2
    @Saswat You should wait and put a bounty on it. Don't repost questions. – Daan May 28 '15 at 10:23

4 Answers4

1

I don't understand the question at all, but maybe you mean something like this?

<?php
// for testing
$post = array (
    "abc" => "123",
    "set_val_1" => 1,
    "set_val_2" => 2
);
$val_string = "";
foreach ($post as $key => $value) {
    if (strpos($key, "set_val_") === 0) {
        $parts = explode("_", $key);
        if (count($parts) != 3) {
            continue;
        }
        if (!is_numeric($parts[2])) {
            continue;
        }
        $val_string .= $value;
    }
}

print_r($val_string);
take
  • 2,202
  • 2
  • 19
  • 36
1

Hi You can achieve this by

$post_keys =  array_keys($_POST);
    $matched_keys = preg_grep('/^set_val_/', $keys);
    if(count($matched_keys) > 0 ){
         $flag = 1;
         $val_string = "";
    }

Although I think approach must be change like this way:

Create array of input elements that you are going to check on server side

<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 on server side you can check this as

      if(isset($_POST['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].",";
                }
            }
        }
Gajendra Singh
  • 636
  • 6
  • 6
0

isset() function will check if your particular $_POST element is actually recieved or not. Format to use it.

if (isset($_POST['form_input_element_name'])){
//rest of your code
}
Sourabh Kumar Sharma
  • 2,864
  • 3
  • 25
  • 33
-1

Use below code,

 if (isset($_POST['submit_button'])){
    //rest of your code
    }

This will show you data is post or not.

Amrut Gaikwad
  • 524
  • 1
  • 3
  • 19