开发者

How can I loop through a series of response variables with a specific prefix in PHP?

开发者 https://www.devze.com 2023-01-22 10:11 出处:网络
I\'m posting a form to a php script. Th开发者_如何学Goe form contains a dynamic number of fields named cardObjectX, where X is a counter. Example: cardObject1, cardObject2, and so on. I need to loop t

I'm posting a form to a php script. Th开发者_如何学Goe form contains a dynamic number of fields named cardObjectX, where X is a counter. Example: cardObject1, cardObject2, and so on. I need to loop through all the cardObject fields in my php script, but because we don't know how many there will be for any given post, we can't hard-code the field names.

Is there a way I can grab an array of all the fields that start with cardObject?


this should help you get started:

foreach($_POST as $key=>$value) {
   if(strpos($key,"cardObject")!==FALSE) {
        //do something with this cardObject...
   }
}


<input name="cardObject[1]" value="">

using this naming style in your inputs makes it possible to access these inputs as an array in php like this:

$_POST['cardObject'][1]

or loop throug every cardObject like this:

foreach($_POST['cardObject'] as $cardObject){

}
0

精彩评论

暂无评论...
验证码 换一张
取 消