Jump to content

Add to Cart functions


bullbreed

Recommended Posts

Hi Guys

 

I have a page on my website that enables a customer to select a product, choose a colour and a size, create some custom text then send that data to my cart.php page. I'm not sure if I am doing this the correct way and haven't done a cart before.

 

This form collects the data then posts it to the cart.php page

 

<form action="cart.php" method="POST">

<input type="hidden" name="category" id="category" value="<?php echo $_GET['cid']; ?>" />
<input type="hidden" name="name" id="name" value="<?php echo $_GET['nid']; ?>" />
<input type="hidden" name="colour" id="colour" value="<?php echo $_GET['colid']; ?>"  />
<input type="hidden" name="input" id="input" value="<?php echo $_POST['input']; ?>"  />
<input type="hidden" name="fontselect" id="fontselect" value="<?php echo $_POST['fontselect']; ?>"  />
<input type="hidden" name="sizeselect" id="sizeselect" value="<?php echo $_POST['sizeselect']; ?>"  />

<div class="textinputgo"><input name="cart" type="submit" id="cart" value="Add to Cart" /></div>

</form>

 

This is the cart.php page which displays the product selections on the page;

( I have used tables for now but will change these to divs.)

 

<h1>Shopping Cart</h1>
<?php

$submit = ($_POST['cart']);

if (!isset($submit)){
echo 'Your cart is empty';
}else {

//Form data
$category = ($_POST['category']);
$name = ($_POST['name']);
$colour = ($_POST['colour']);
$text = ($_POST['input']);
$font = ($_POST['fontselect']);
$size = ($_POST['sizeselect']);

if (isset($submit)){

// This page displays the contents of the shopping cart.
        
        // Create a table and a form.
echo '<table border="1" width="90%" cellspacing="3" cellpadding="3" align="left">

        <tr>
                <td align="left" width="15%"><b>Category</b></td>
                <td align="left" width="15%"><b>Name</b></td>
                <td align="left" width="15%"><b>Colour</b></td>
                <td align="left" width="15%"><b>Text</b></td>
                <td align="left" width="10%"><b>Font</b></td>
                <td align="left" width="15%"><b>Size</b></td>
        </tr>
';

echo '<tr>';
echo '<td align="left">' . $category . '</td>';
echo '<td align="left">' . $name . '</td>';
echo '<td align="left">' . $colour . '</td>';
echo '<td align="left">' . $text . '</td>';
echo '<td align="left">' . $font . '</td>';
echo '<td align="left">' . $size . '</td>';
echo '</tr>';

echo '</table>';
}
}

?>

 

My questions are;

 

1. How do I add the facility to increse the quantity of the products on the cart.php page?

 

2. If I go to another page then click on cart again the cart is empty so how do I keep the info in the cart? I'm assuming some sort of session but dont know how to do these.

 

Thanks guyz

Link to comment
Share on other sites

1. How do I add the facility to increse the quantity of the products on the cart.php page?

 

name="products[$ID]" then when the user presses UPDATE you go over each item and check if the quantity has changed if it did then modify it in your database.

 

2. If I go to another page then click on cart again the cart is empty so how do I keep the info in the cart? I'm assuming some sort of session but dont know how to do these.

 

$_SESSION['cart_namespace'][$productID] = $row;

Link to comment
Share on other sites

$result = mysql_query("SELECT * FROM cart WHERE session_id = $sid OR user_id = $uid");

$products = array();
while ($row = mysql_fetch_assoc($result)) {
    $products[$row['id']] = $row;
    echo '<input type="text" name="product[', $row['id'], ']" value="', $row['quantity'], '">';
}

//..
if (isset($_POST['product']) && is_array($_POST['product'])) {
    foreach ($_POST['product'] as $id => $quantity) {
        $id = intval($id);
        $quantity = intval($quantity);
        if (isset($products[$id])) {
            if ($quantity <= 0) {
                mysql_query("DELETE FROM table WHERE user_id = $uid AND id = $id");
            } else if ($quantity != $products[$id]['quantity']) {
                mysql_query("UPDATE table SET quantity = $quantity WHERE id = $id");
            }
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.