tags:

views:

23

answers:

2

I'm posting a form to a php script. The 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?

+1  A: 
<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){

}
ITroubs
if you can't change the naming convetion of your field then you have to loop through the whole $_POST var like this foreach($_POST as $varname=>$input) and check whether $varname contains the string 'cardObject'
ITroubs
Nope, can't change the naming convention. I figured I'd have to loop through the whole post, I just wasn't quite sure how.
EmmyS
+1  A: 

this should help you get started:

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