PHP Iterate through $_POST and use values by name

前端 未结 4 1417
醉酒成梦
醉酒成梦 2021-02-05 05:14

I have a form that contains a number of fields with names item1, item2, item13, item43 etc, each time those fields are different because they are populated in the form with AJAX

4条回答
  •  佛祖请我去吃肉
    2021-02-05 05:54

    You can loop through $_POST with foreach like this:

    foreach ($_POST as $key => $value) { ... }
    

    And within the loop you can evaluate whether each key found by the loop matches your criteria. Something like this:

    foreach ($_POST as $key => $value){
       if (substr($key, 0, 4) == "item") {
          $identifier = substr($key, 4);
          if (isset($value['tag' . $identifier])) { inserttag('tag', $identifier); }
       }
    }
    

    I'm not 100% sure what is actually real and what is just a placeholder in your question though. Maybe I took something for solid fact that actually isn't. You might need to explain your wishes in more detail. ;)

提交回复
热议问题