Learn how to a build a cloud-first strategyRegister Now


Form Posting while Looping through arrays

Posted on 2006-04-12
Medium Priority
Last Modified: 2008-02-26

I am trying to make a modification to my X-Cart shopping cart.  I am trying to setup a page that displays all of my products and a shopper can say that they want 2 of this product, 3 of these, 5 of these... When they are all done, they click add to cart and it adds all of their selections.  Now I have the page setup, but I need to get each element from these arrays to post seperately.  For each item purchased, their are 4 input's that go along with it.  SO, I need it to post those four items then go to the next set and post those four...and so on and so forth. Here is the code for my input types:
<INPUT type="hidden" name="productid[]" value="<?php echo $x_productid;?>">
<INPUT type="hidden" name="mode[]" value="add">
<input type="hidden" name="product_options[]" value="<?php echo $x_optionid ;?>">
<input type="text" name="amount[]" size="5" maxlength="3" value="">
To sum it up...I need productid[0],mode[0],prouduct_options[0], amount[0] to all post to a cart.php, then loop through with arrays[1]..

Your help is much appreciated.
Question by:koolie
  • 2
LVL 15

Expert Comment

ID: 16438766
well i m not clear with what you want
but here is a suggestion
For each product use a check box and then only those checked boxes will be passed to cart.php
for those checked boxes values get the corresponding values from the productid[checkboxvalue],mode[checkboxvalue],prouduct_options[checkboxvalue], amount[checkboxvalue]

Expert Comment

ID: 16438842
are you mean somethings like

and etc, in your php scripts?


Author Comment

ID: 16438882
to blue_hunter:
something like that,but x-cart requires me to send it the value $mode so any array numbering and their script wont recognize it.  so i need to loop through the array and then reassign its name. I could send cart.php the array and then process the loop, but sending $_POST["mode][any number] wont work.

Accepted Solution

blue_hunter earned 750 total points
ID: 16442132
as we mention before, you can get the value with

now, to loop through the variable and reassign the values in the array to another array
example code:

$productid = array();
foreach($_POST["productid"] as $k => $d){
$productid[] = $d;

same goes to others.

hope this clearly answer your question

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question