Form Posting while Looping through arrays

Posted on 2006-04-12
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
    LVL 15

    Expert Comment

    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]
    LVL 9

    Expert Comment

    are you mean somethings like

    and etc, in your php scripts?

    LVL 5

    Author Comment

    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.
    LVL 9

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now