0

I have a form in my PHP page which is created by a loop through an array.

echo '<form method="POST" name="add_to_cart_form">
        <div id="Product_add_sizes">
           <table border="0" cellpadding="2" class="product_txt_font" align="center">
              <input type="hidden" name="product_id" value="'.$arr_get_products[$c]['id'].'">';

                   for ($d = 0; $d < count($arr_get_product_details); $d++)
                   {
                      echo '<tr>
                              <td>
                                 <input type="radio" name="size[]" value="'.$arr_get_product_details[$d]['size'].'">
                              </td>
                              <td>
                                 <input class="qty" type="text" size="3" name="amount" value="1">
                              </td>
                            </tr>';
                   }

     echo '</table>
          <input type="submit" name="add_to_chart" value="Add Product" />
        </div>
      </form>';

Now when I post this form, I'm searching for a way to get the input of qty which belongs to the selected radio button. I only need the data from the selected radio button row. The other data is not needed as I want to add this product to a shopping cart array.

if (isset($_POST['add_to_chart']))
{
    $product_id = $_POST['product_id'];
    $size = $_POST['size'][0];
    $qty = $_POST['amount'];
}

When posting the page, I know what size is wanted cause of the size[] array but I can't get the related qty value that matches the selected radio button.

I've tried to treat the qty the same way as the radio button by making it an array qty[] but that will return me all values.

I've search SO and googled a bunch but haven't found a decent answer, but It looks to me that this is used a lot. Please let me know what i'm missing here.

Any help is greatly appreciated!

4
  • 1
    name="size['.$d.']" will define the index it relates to, you also need to name="amount['.$d.']" on other input to match them up Commented Aug 17, 2012 at 10:59
  • $_POST['amount'] qty is the class! Commented Aug 17, 2012 at 11:02
  • Yea it was a type :) should be amount Commented Aug 17, 2012 at 11:02
  • @Fluffeh points out the real answer, it just that 'amount' was in the loop too! Commented Aug 17, 2012 at 11:04

4 Answers 4

1

Radio buttons should all use the same name. Only the selected radio button value is submitted:

<input type="radio" name="Radiosize" value="'.$arr_get_product_details[$d]['size'].'">

then:

$size = $_POST['Radiosize'];

There is no need to look at it as an array - it isn't submitted as one. Radio Buttons are not like checkboxes in form processing.

Sign up to request clarification or add additional context in comments.

Comments

1

change

<input class="qty" type="text" size="3" name="amount" value="1">

to

<input class="qty" type="text" size="3" name="amount[]" value="1">

and then you will have 2 arrays that will have same size.

<?php
    $size = $_POST['size'][0];
    $amount = $_POST['amount'][$size];
?>

Comments

1

Form code:

    <input type="radio" name="size" value="'.$arr_get_product_details[$d]['size'].'">
    <input class="qty" type="text" size="3" name="amount['.$arr_get_product_details[$d]['size'].']" value="1">

And then you will have value of size. And array of amounts with size keys.

    $size   = $_POST['size'];
    $amount = $_POST['amount'][$size];

Comments

0

I solved the issue by doing the following.

The guys above here all got me on the right track and couldn't have done it without them!

Changed

<input type="radio" name="size[]" value="'.$arr_get_product_details[$d]['size'].'">
<input class="qty" type="text" size="3" name="amount" value="1">

To

<input type="radio" name="size['.$d.']" value="'.$arr_get_product_details[$d]['size'].'">
<input class="qty" type="text" size="3" name="amount['.$d.']" value="1">

Also changed

if (isset($_POST['add_to_chart']))
{
    $product_id = $_POST['product_id'];
    $size = $_POST['size'][0];
    $qty = $_POST['amount'];
}

To this

if (isset($_POST['add_to_chart']))
{
    // Array ID key
    $key = key($_POST['size']);

    $product_id = $_POST['product_id'];
    $size = $_POST['size'][$key];
    $qty = $_POST['amount'][$key];
}

Works like a charm! Thank you all for your very helpful comments!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.