Hi there,
I am struggling with a bit of code and need a bit of help please..

I have information in a database that I want to display in a table, then allow users to choose a number of items and click 'add to cart'.

It works fine if I include the 'submit' button within the loop but this means the user has to click 'add to cart' under every item.... Ideally I want 1xSubmit button in the last column which would add all the selected items to cart. I have tried the code below but it only adds the last item of the loop into the cart.

Please help!

Thanks,
Dave

` echo "<table>";
echo "<td>Choose items</td>";

while($row = mysql_fetch_array($result))  {

    echo "<form action='add-to-cart.php' method='post'>";

    echo "<td>".$row['id']."'><br>";
    echo "<input type='hidden' name='propornot' value=".$row['propornot']." />";
    echo "<input type='hidden' name='pid' value=".$row['id']." />";
    echo "<input type='radio' name='quantity' value='1'> 1 item<br>";
    echo "<input type='radio' name='quantity' value='2'> 2 items<br></td>";
}

    echo "<td><input type='submit' name='submit' value='add to cart' class='fw' /></form></td>";
    echo "</table>";

    `

Take your <form> tag out of the loop bud. Your initializing a new form with every item you add in the loop, but you're closing it only once outside the loop .

Also, to help you further, show us your add-to-cart.php code.

Hi there,
Really appreciate your speedy reply and help but I still can't get it to work.
I have moved the <form> outside of the loop to just after the <table>.
It selects the correct quantity value (1 or 2) but it still selects the last item of the loop and inputs into cart.

Please see below all functions and process of add to cart.

I have about 5 items that will populate the table and I would like the user to add thier choice of items to cart with quantities of 1 or 2.

Thank you for your time.
Dave

`

$returnPage = $_SERVER['HTTP_REFERER'];

//submit button check
if (!isset($_POST['submit'])) {
    redirect($returnPage);  
}

$itemDetails = processAddToCartForm($_POST);



if (db_addItemToCart($itemDetails, session_id() ,$cn)) {   //add the item and the session id to the database
    redirect($returnPage);
} else {
    setMessage ("Item was not added to cart");      
    redirect($returnPage);  
}




function processAddToCartForm ($formData) {
foreach ($formData as $key => $value) {
    switch ($key) { 
        default : 
            $local[$key] = sanitiseInput($value);
    }
}
return $local;

}



function db_addItemToCart($product, $session, $connection) {


foreach ($product as $key => $value) {
    $$key = $value;                 //it is okay to use local variables within a function       
}


$sql = "SELECT price AND propornot FROM products WHERE id = $pid";
$result = mysql_query($sql, $connection);   
$numRows = mysql_num_rows($result);


if ($numRows >= 1) {
    //product exists in database
        if ($pid !='20' && $row = db_itemInCart($pid, $session, $connection)) {
            $addSql = "UPDATE cart_items SET quantity = quantity + $quantity WHERE id = $row";          
        } else {
            $addSql = "INSERT INTO cart_items (product_id, session_id, quantity, propornot) VALUES ($pid, '$session', $quantity, '" . ($_POST['propornot']) . "')";
        }
        $result = mysql_query($addSql, $connection);    
        $numRows = mysql_affected_rows($connection);
        if ($numRows == 1) {
            return true;
        } else {
            return false;
        }

} else {
    return false;   //product was not found - do not add to cart
}   

}


function db_itemInCart($product, $session, $connection) {       
    $sql = "SELECT id FROM cart_items WHERE session_id = '$session' AND product_id = $product";
    $result = mysql_query($sql, $connection);   
    $numRows = mysql_num_rows($result);
    if ($numRows >= 1) {
        $item = mysql_fetch_assoc($result);
        return $item['id'];
    } else {
        return false;
    }
}


`

Whops, my apologies for a half-hearted response last time. I just saw what the issue is. Yep you had to move the <form> outside which you did, so that's good. The reason you're only getting the last value with your script is because you have no way to differentiate between the "name" of your radio buttons. They're all called "quantity", so in essense, the "quantity" keeps resetting itself while it goes through the loop, and eventually stops with the last item in the loop. As a matter of fact, look at your HTML form. You shouldn't even be able to select more than one value at a time for all 10 radio buttons now, even though they're showing up as five different items (2 radio buttons for each item). The only reason you were able to do that before was because you had a new form generating with every loop, and ofcourse we already established that was a bad move especially since you want one submit button submitting everything.

So what you need to do is:

// These two lines are fine
echo "<input type='hidden' name='propornot' value=".$row['propornot']." />";
echo "<input type='hidden' name='pid' value=".$row['id']." />";

// Use your $row['id'] to uniquely identify each quantity
echo "<input type='radio' name='quantity".$row['id']."' value='1'> 1 item<br>";
echo "<input type='radio' name='quantity".$row['id']."' value='2'> 2 items<br></td>";

Now edit your php accordingly to pull those unique quantity values for each item.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.