Results 1 to 8 of 8

Thread: [RESOLVED] PHP generated forms

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Resolved [RESOLVED] PHP generated forms

    I have a form generated by PHP with an unknown number of textfields on this data is then posted to a script how can I loop through all of the text fields on the form?

    Thanks

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP generated forms

    If you give them all the same name followed by a pair of brackets [] PHP will automatically create an array for you containing all of the values.

    PHP Code:
    <form action="wherever" method="post">
      <
    input type="text" name="values[]" value="1">
      <
    input type="text" name="values[]" value="2">
      <
    input type="text" name="values[]" value="3">
    </
    form
    You can access the posted data using $_POST['values'][n].


    If you cannot change the names of the text fields then you will have to loop through $_POST.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: PHP generated forms

    Thanks pengate

    How do I find the upper bound of the array?

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP generated forms

    count($array) returns the length and the upper bound is 1 below that.

    You can use either foreach() or for() to enumerate the array.
    PHP Code:
    foreach ($array as $value) { ... $value ... };
    foreach (
    $array as $key => $value) { ... $value or $array[$key] ... };
    for (
    $i 0$i count($array); ++$i) { ... $array[$i] ... }; 

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [RESOLVED] PHP generated forms

    For example I have the following,

    PHP Code:
    "<form name='form1' method='post' action='makeOrder.php'>";

    //Then a number of input boxes.

    <input name='quantity[]' type='text' size='6'
    For my example there is 3 boxes, I input data into them all and if I do this....

    PHP Code:
    $quantityArray[] = $_POST['quantity'];

    Echo 
    count($quantityArray); 
    I'm only getting the number 1 outputted!

    Thanks

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [RESOLVED] PHP generated forms

    Echo count($_POST['quantity']);

    But that does seem to work, so my assignment to the array is wrong, hmm its agers since I've used php! What am I missing?

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] PHP generated forms

    The brackets are not needed:
    PHP Code:
    $quantityArray $_POST['quantity']; 
    What the assignment $x[] = $y does is append $y to the end of the array $x. If $x does not already exist, $x will be created with $y as its sole offset; hence count($x) returns 1 and the actual array is being stored at $x[0].


    Edit: I spent ages writing a reply to that other post, and then I couldn't figure out where it had gone when I previewed.
    Last edited by penagate; May 12th, 2007 at 01:32 PM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [RESOLVED] PHP generated forms

    Thanks, sorry I relised I was spamming in my random thoughts Thanks pengate helped a lot. I'll see if I cna finish this up now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width