Form in a loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

Form in a loop

Post by rocknrisk »

Hi all,

I need help bad here please. I trying to write a shopping cart, specific to me company.

How does one set variables when looping through php generated html?.. i.e:

I have an array

Code: Select all

$arr = array(1, 2, 3);        // would be a query from a database.
foreach ($arr as $v) {
	fctShowItem ($v);
}
Then I create the listing (with html formating)

Code: Select all

<?php
	function fctShowItem ($v) {
?>
... HTML code...
<tr><form name="form<?php echo $v; ?>"
         method="post"
         action="test.php">
    <td width="63" valign="middle">
       <input name="butRemove"
       type="submit"
       class="style14"
       id="butRemove"
       value="Remove">
    </td>
    <td width="86" height="20" valign="middle"><div align="right"><span class="style16"> Qty :</span> 
       <input name="textfield" type="text" class="style14" size="3" maxlength="3">
    </div></td>
    <td width="115" valign="middle"><div align="right"><span class="style16">Size :</span>
       <input name="textfield2" type="text" class="style14" size="10" maxlength="10">
    </div></td>
    <td width="10" valign="middle">&nbsp;</td>
    <td width="57" valign="middle"><div align="right">
       <input name="butUpdate"
            type="button"
            class="style14"
            id="butUpdate"
            value="Update">
     </div></td>
     <td width="54" valign="middle"><div align="right" class="style4">$Price</div></td>
</form>
</tr>
... A bit more HTML code, then ...

<?php
	} // end fctShowItem
?>
Please ignore the few other variables placemarkers, I've copied and pasted this code from my work.

So how do setup the Remove and Update buttons for each item in the list. I know how to write the code to do the actual removing and updating, but how do set up the buttons themselves to work on it's own record.

Normally I'd use

Code: Select all

$butRemoveItem = isset($_REQUEST['butRemove']) ? true : false;

if ($butRemoveItem) {
   // do something
}
(for the remove button for example).

I hope this makes sense. I am desperate... Any help or direction (I have looked all over the internet, trust me, whew) will be greatly appreciated.

Thank you in advance.

Clinton
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Store the item number as a hidden field in the form. Everything else functions as per normal from what I can see.
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

Form in Loop

Post by rocknrisk »

Thank you feyd, but how do I do that, please?

I still will have a variable amount in the listing. The array was just to illustrate my question.

so I may have form1, form2... ect

You would normally use

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "from1")) { 
   // do something
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "from2")) { 
   // do something else
}
wouldn't you?

Crazy. Pulling my hair out here. I've even thought about somehow creating an array... not sure how though... maybe

Code: Select all

$arrForms[] = 'form.$v;
in each loop...
rocknrisk
Forum Newbie
Posts: 16
Joined: Tue Mar 14, 2006 8:09 pm

Sorry...

Post by rocknrisk »

I do mean form1 and form2, not from1 and from2...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was suggesting something more like

Code: Select all

echo '<input type="hidden" name="recordId" value="' . $v . '" />';
Post Reply