kenoli Posted June 3, 2008 Share Posted June 3, 2008 I am trying to capture the data from a form with several fields in a way that I can update a number of database records, each with several fields, from one form submittal. I can capture the data from one of the form inputs like this: <?php if ($_POST['Submit']) { foreach($_POST['test'] as $key => $value) { echo $value . '<br/>'; } } ?> <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'] ?>"> Test: <input type="text" name="test[]" id="textfield"><br> When: <input type="text" name="when[]" id="textfield"><br> Test: <input type="text" name="test[]" id="textfield"><br> When: <input type="text" name="when[]" id="textfield"><br> Test: <input type="text" name="test[]" id="textfield"><br> When: <input type="text" name="when[]" id="textfield"><br> Test: <input type="text" name="test[]" id="textfield"><br> When: <input type="text" name="when[]" id="textfield"><br> Test: <input type="text" name="test[]" id="textfield"><br> <input name="Submit" type="Submit"> </form> Is there a way I can also capture the data from $_POST['when'] or even more similar field arrays? I'm sure there is a way to do this but can't see it. I would greatly appreciate help figuring this out. Thanks, --Kenoli (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/108474-looping-through-multiple-arays/ Share on other sites More sharing options...
mushroom Posted June 3, 2008 Share Posted June 3, 2008 I didn't know browsers had the ability to build arrays the same way php can ??? if they do, try designating each array value when[0], when[1], when[2], etc. Link to comment https://forums.phpfreaks.com/topic/108474-looping-through-multiple-arays/#findComment-556201 Share on other sites More sharing options...
dsaba Posted June 3, 2008 Share Posted June 3, 2008 Sure. The $_POST array in PHP can handle javascript arrays of post variables. I tried this code: <?php if (count($_POST) > 0) { echo '<pre>'; print_r($_POST); echo '</pre>'; } ?> <form name="form1" method="post"> Test: <input type="text" name="test[]" id="textfield"> When: <input type="text" name="when[]" id="textfield"> Test: <input type="text" name="test[]" id="textfield"> When: <input type="text" name="when[]" id="textfield"> Test: <input type="text" name="test[]" id="textfield"> When: <input type="text" name="when[]" id="textfield"> Test: <input type="text" name="test[]" id="textfield"> When: <input type="text" name="when[]" id="textfield"> Test: <input type="text" name="test[]" id="textfield"> <input name="Submit" type="Submit"> </form> And got this output after entering numbers 1-9 in left to right across the form fields: Array ( [test] => Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 ) [when] => Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 ) [submit] => Submit Query ) You can loop through each $_POST item and then loop through its own array: <?php foreach ($_POST as $key => $val) { if (is_array($val)) { foreach ($val as $k => $v) { echo "$k => $v<br>"; } } else { echo "$key => $val<br>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/108474-looping-through-multiple-arays/#findComment-556206 Share on other sites More sharing options...
kenoli Posted June 4, 2008 Author Share Posted June 4, 2008 Great! This gets me going in some good new directions. I'll check back in if I can't find a solution. Thanks, --Kenoli Link to comment https://forums.phpfreaks.com/topic/108474-looping-through-multiple-arays/#findComment-557087 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.