post - How to define an array of form fields PHP -
i have question.
i have code in first page:
<input type="text" name="number" id="number"> <input type="submit" name="button_add" id="button_add" value="add"> <? $i=0; while($number>$i) { $i++; ?> <div align="left"> <input type="text" name="text[<? echo $i; ?>]" id="text[<? echo $i; ?>]" /><? } ?> how define array of fileds in next page using $_post vars?
the entry in $_post array, if that's you're asking:
for ($i = 0; $i < count($_post['text']); ++$i) { // post: $_post['text'][$i]; } or:
foreach ($_post['text'] $i => $text) { // with: $text; // note: $text === $_post['text'][$i] } off-topic
always close elements:
<input type="text" name="number" id="number" /> <input type="submit" name="button_add" id="button_add" value="add" /> you neglected close <div> wrapping text[] inputs. if don't, document ill-formed and, though browsers attempt parse document, there's no guarantee they'll way want.
the default type inputs text. doesn't hurt set type attribute "text", isn't necessary.
don't rely on short tags; use full <?php. not hosts have them enabled, , they're deprecated , going away (there's been talk doing away them before php 5).
you don't need explicitly assign array indices input names; empty brackets cause value assigned end of array. also, '[' , ']' aren't valid characters ids.
<input type="text" name="text[]" id="text_<?php echo $i; ?>" /> a for loop more appropriate while loop in code. though both same thing, have different connotations (a while loop repeats while static condition holds under changing circumstances; loop repeats on sequence).
for ($i=0; $i < $number; ++$i) { all together, have:
<input name="number" id="number" /> <input type="submit" name="button_add" id="button_add" value="add" /> <?php ($i=0; $i < $number; ++$i): ?> <div align="left"> <input name="text[]" id="text_<?php echo $i; ?>" /> </div> <?php endfor; ?>
Comments
Post a Comment