0

okay, im new in this site also new in php and can't get the logic on this,

i have a product page that shows the name, quantity and an add to cart button in each row of product

i made this just cutted of some code

    while($showProducts = mysql_fetch_array($products))
            {
            $currenQuantity = $showProducts['current_quantity'];
            $prodid = $showProducts['product_id'];
    echo"<select name='quan'>";

                for ($x=0;$x<=$currenQuantity;$x++)
            {
                if($currenQuantity != 0)
                {
                echo "<option value=$x> $x </option>";
                }
            }
     echo"</select><br/>";
     }

now the problem is every time i tried to get the value by using $_POST['quan'] the value that i always get is the default value 1 even i select a different value of quantity of a certain product, and i'm blanked with ideas.

2
  • Where are you using $_POST['quan']? You mention that you've cut some code, but that seems relevant... Commented Jul 24, 2013 at 15:08
  • i put it in a variable everytime i clicked button add to cart like here. Michael's solution worked for me. code if(isset($_POST['addCart'])) { $prodId = $_POST['addCart']; $_POST['quan']; Commented Jul 24, 2013 at 15:32

3 Answers 3

2

You can't use the same name for an input/select field in a form. You have to specify a diffrent name or create an indexed array:

<select name="quan[$prodid]">

You can acces it via

$_POST['quan'][$prodid]
Sign up to request clarification or add additional context in comments.

Comments

0

Just a wild guess: has each of your product rows a select named 'quan'? Then you're getting the last select named 'quan' in your POST data.

Prefix each of the quantities selects with name of the product, or some kind of a (scrambled) id.

Additionally: If you don't want to have a 0 in your select, don't start the for loop with 0

for ($x=1;$x<=$currenQuantity;$x++)

if the current quantity is 0, the for loop doesn't get executed.

1 Comment

thanks, I fixed that mistake, yes you're right i always get the value of the last dropdown , btw I solved my problem now.
0

If you have more selectboxes with the same name (quan) in one form, you are getting value of the last one.

You should change the selectbox definition to:

echo"<select name='quan[" . $prodid . "]'>";

then you can access the values using:

$_POST["quan"][$some_product_id]
$_POST["quan"][$another_product_id]

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.