0

I have a question.

I have this code in the 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 do I define the array of fileds in the next page using $_POST vars?

Josh
  • 10,961
  • 11
  • 65
  • 108
Victor
  • 99
  • 1
  • 6
  • 15
  • 1
    Wow ... I think I need a drink or two to understand that one. Can you rephrase your question? Are you trying to pass an array of values via the post action, or ... what are you trying to do here? – Luke Stevenson Dec 03 '10 at 12:39
  • sorry I am not orientatied in the sistem hear. That's why my raitings are so bad. – Victor Dec 03 '10 at 12:47
  • simply go to your profile page by clicking on your username in the top menu bar (or anywhere else for that matter) and start going through your previous questions and mark the answers that helped you solve your problem as accepted by clicking the check-mark below an answer's score. – Valentin Flachsel Dec 03 '10 at 12:52

3 Answers3

4

The entry in $_POST is itself an array, if that's what you're asking:

for ($i = 0; $i < count($_POST['text']); ++$i) {
    // do something with this post:
    $_POST['text'][$i];
}

or:

foreach ($_POST['text'] as $i => $text) {
    // do something with:
    $text;
    // Note: $text === $_POST['text'][$i]
}

Off-topic

Always close your elements:

<input type="text" name="number" id="number" />
<input type="submit" name="button_add" id="button_add" value="add" />

You also neglected to close the <div> wrapping the text[] inputs. If you don't, your document is ill-formed and, though browsers will attempt to parse the document, there's no guarantee they'll do it the way you want.

The default type for inputs is text. It doesn't hurt to set the type attribute to "text", but it isn't necessary.

Don't rely on short tags; use the full <?php. Not all hosts will have them enabled, and they're deprecated and will be going away soon (there's been some talk about doing away with them before PHP 5).

You don't need to explicitly assign array indices for your input names; empty brackets will cause the value to be assigned to the end of the array. Also, '[' and ']' aren't valid characters for IDs.

<input type="text" name="text[]" id="text_<?php echo $i; ?>" />

A for loop is more appropriate than a while loop in your code. Though both do the same thing, they have different connotations (a while loop repeats while some static condition holds under changing circumstances; a for loop repeats over a sequence).

for ($i=0; $i < $number; ++$i) {

All together, we have:

<input name="number" id="number" />
<input type="submit" name="button_add" id="button_add" value="add" />
<?php for ($i=0; $i < $number; ++$i): ?>
  <div align="left">
    <input name="text[]" id="text_<?php echo $i; ?>" />
  </div>
<?php endfor; ?>
Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221
  • sorry for the dull question but this lines I must enter in the code above or in the next page. – Victor Dec 03 '10 at 12:41
  • @Victor: You would use this code in the page which is processed when the post submission happens. So if your form is submitting to `filename.php`, then that is the file you would use @outis' code in. – Luke Stevenson Dec 03 '10 at 12:48
3

Try:

foreach( $_POST['text'] as $key => $value){
 echo "Value for texbox # ".$key." is:". $value;
    // or whatever code you want
}

In the page the form submits to.

SW4
  • 69,876
  • 20
  • 132
  • 137
-2

I'm on a netbook and it would be apain to write the code let alone test it.

use a loop and the post var and isset function.

Yehonatan
  • 1,172
  • 2
  • 11
  • 21
  • 2
    That's hardly an answer regardless if you are keyboard/machine-impaired or not. – Valentin Flachsel Dec 03 '10 at 12:46
  • 1
    You should see the size of my digits! At least after some rough use the keyboard has loosened up abit. But still, its real tight. Next time when I'm banging away I'll add some pseudo code to make it easier for you. – Yehonatan Dec 03 '10 at 18:47