Re: Form Loop

From: Date: Sun, 19 Oct 2008 08:40:40 +0000
Subject: Re: Form Loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-282094@lists.php.net to get a copy of this message
I'm trying to create a form with a loop. I need to append a value to a field name each time through the loop. For Instance: while ($row = mysql_fetch_assoc($result)) {
    $x=1;
    echo "<tr>";    echo "<td><input type='text' id='qty' name='quantity_'  size='2' value='$row[qty]' /></td>";
?>
<td><input type="text" id="qty[<?php echo $x; ?>]" name="quantity[<?php echo $x; ?>]" size="2" value="$row[qty]"/></td> <?php
    echo "</tr>";
    $x++;
} the name value quantity needs the value of x appended to it. quantity_1, quantity_2 etc. I recommend using array notation instead of appending $x to the element name. If you are using POST, when the form is submitted the values are available in the $_POST['quantity'] array. You can use foreach to iterate through it.
Example: <?php $values = $_POST['quantity']; foreach ($values as $value) { echo $value; } ?> Regards, -- With warm regards, Sudheer. S Business: http://binaryvibes.co.in, Community: http://lampcomputing.com, Personal: http://sudheer.net

« previous php.general (#282094) next »