Good day,
I am a PHP beginner, but have managed to set up a shopping cart on my jewelry website (with the help of video tutorials found online). Everything is working well, but I need to add a text box to my product page where people can add the engraving they would like to have on the specific product. This information then needs to be sent to the cart together with all the other details when they press Add to cart.
This is the Product page: Click Here
My product.php page has the following code:
<div class="box1">
<table width="100%" border="0" cellspacing="0" cellpadding="15" align="center">
<tr>
<td width="25%" valign="top">
<img src="inventory_images/<?php echo $id; ?>.jpg" width="200" height="200" alt="<?php echo $product_name; ?>" /><br />
<a href="inventory_images/<?php echo $id; ?>.jpg">View full size</a></td>
<td width="75%" valign="top">
<h3><?php echo $product_code; ?></h3>
<?php echo $product_name; ?><br />
<br />
<?php echo $details; ?><br />
<br />
<?php echo "R".$price; ?><br />
<br />
<form id="form1" name="form1" method="post" action="cart.php">
<label for="engraving">Engraving</label><br />
<input type="text" name="engraving" id="engraving" value="" size="35" /><br />
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /><br />
<input type="submit" name="button" id="button" class="submit" value="Add to cart" />
</form>
</td>
</tr>
</table>
</div>
The "engraving" field is what I need to be sent to cart.php and be displayed on in the cart's Engraving column.
Right now the Engraving column is empty and I get an Notice: Undefined index: engraving in /var/www/vhosts/personalizedjewellery.co.za/httpdocs/store/cart.php on line 13 error.
This is my cart.php code:
<?php
session_start();
// Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
//Section 1 (if user attempts to add something to the cart from the product page)//
///////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$engraving = $_POST['engraving'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "engraving" => $engraving, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>
<?php
///////////////////////////////////////////////////////
//Section 3 (if user chooses to adjust item quantity)//
///////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
// execute some code
$item_to_adjust = $_POST['item_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
if ($quantity >= 100) { $quantity = 99; }
if ($quantity < 1) { $quantity = 1; }
if ($quantity == "") { $quantity = 1; }
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $item_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
}
?>
<?php
/////////////////////////////////////////////////////////////////
//Section 5 (render the cart for the user to view on the page)//
/////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h3 align='center'>Your shopping cart is empty</h3>";
} else {
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_code = $row["product_code"];
$product_name = $row["product_name"];
$price = $row["price"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
$pricetotal = money_format("%.2n", $pricetotal);
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].", ";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_code . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_code. '" width="75" height="75" border="1" /></td>';
$cartOutput .= '<td>' . $engraving . '</td>';
$cartOutput .= '<td>R' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /><br />
<br />
<input name="adjustBtn' . $item_id . '" type="submit" class="submit" value="Change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>R' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" class="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
$cartTotal = money_format("%.2n", $cartTotal);
$cartTotal = "R".$cartTotal." ";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Your Cart</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="storescripts/gen_validatorv4.js" type="text/javascript"></script>
</head>
<body>
<?php include_once("storescripts/analyticstracking.php") ?>
<div id="page-container">
<div id="header">
<?php
include "storescripts/header.php";
?>
</div>
<div id="center-panel">
<div class="box1">
<?php
include "storescripts/menu.php";
?>
</div>
<div class="box1">
<h3>If you buy 6 items, you will receive the 6th item for free.</h3>
We will make the necessary changes to your invoice before sending it to you.<br />
<br />
<h3>Postage</h3>
Shipping cost needs to be added to the shopping cart's total.<br />
You only pay once for shipping regardless of how many items you order.<br />
<br />
<br />
<center>
<img src="images/cart.png" alt="Cart" title="Cart" border="0" />
<h3>Your Cart</h3>
</center>
<div style="margin:24px; text-align:left;">
<table width="80%" border="1" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="20%" bgcolor="#FFFFFF"><strong>Product</strong></td>
<td width="35%" bgcolor="#FFFFFF"><strong>Engraving</strong></td>
<td width="10%" bgcolor="#FFFFFF"><strong>Price</strong></td>
<td width="10%" bgcolor="#FFFFFF"><strong>Quantity</strong></td>
<td width="10%" bgcolor="#FFFFFF"><strong>Total</strong></td>
<td width="10%" bgcolor="#FFFFFF"><strong>Remove</strong></td>
</tr>
<?php echo $cartOutput; ?>
</table>
<br />
<center>
<b>Total: <?php echo $cartTotal; ?></b><br />
</center>
<br />
Can someone please help me with this? Will be forever grateful as I am completely stuck.
Thanks in advance,
Susan