Welcome to WebmasterWorld Guest from 34.229.126.29

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Sending a form input to shopping cart. please help

     
8:29 am on Aug 3, 2015 (gmt 0)

New User

joined:Aug 3, 2015
posts:1
votes: 0


Good day everyone,
I am a complete beginner when it comes to PHP, but have managed to set up a shopping cart (with the help of video tutorials I found online). Everything is working perfectly, but I would like to add something and I'm having SO much trouble getting it to work. I'm sure it will be an easy fix and would appreciate any help I can get.
I have a product.php and a cart.php file. The product.php file gets all the product's info from the database. I sell personalized jewelry and I need for people to be able to add the engraving they would like into a text box that gets sent to the cart along with all the product's info.

On the product.php file I have the following form:

<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 value needs to be sent to cart.php and be displayed in the cart's table. Right now nothing is sent over, the Engraving column stays empty and I get an error that says Notice: Undefined index: engraving in /var/www/vhosts/personalizedjewellery.co.za/httpdocs/store/cart.php on line 13
I know engraving needs to be declared, but I don't know where and how! :(

My cart's code is:

<?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 2 (if user chooses to empty their shopping cart)//
////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>

<?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 4 (if user wants to remove an item from cart)//
/////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
}
?>

<?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 />


If anybody can point me in the correct direction, I would really appreciate it.

Thank you in advance,
Susan
1:33 pm on Sept 2, 2015 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 15, 2004
posts:942
votes: 0


The error you get tells you that the form is not submitting the field in the page
This means that there is no $_POST['engraving'] whatsoever.

Above the line you get the code, add the follwoing
echo '<pre>';
print_r($_POST);

This will tell you what is being sent to your cart.php.
From there try to debug you code and see what is happening (most common scenarios include spelling errors, unwanted spaces in names (form field or POST array), or... submitting to wrong page/url/folder/class/function/whatever

I can not offer much more help. This is something you must track yourself and fix alone.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members