I'm hoping someone can help me with this. I'm trying to pass a javascript variable to php for my php shopping cart. What I have is a gift basket that has 4 products in it and the user selects a scent for each from a drop down menu. I have the selections appearing in a textarea box like this:
Foaming Bath Butter 8oz: Mango Papaya
Body Scrub 8oz: Pomegranate
Body Mist 150mL: Bamboo & Teak
Hand Cream 60mL: Unscented
I have the textarea contents saved in a variable "description." The user then clicks on "Confirm" to confirm the scents which creates the url:
giftBox50.php?&scent=Foaming Bath Butter 8oz: Mango Papaya Body Scrub 8oz: PomegranateBody Mist 150mL: Bamboo & TeakHand Cream 60mL: Unscented
I then use the $_GET['scent'] to retrieve the scents. When the user clicks "add to cart" the url displays:
cart.php?id=11&pid=11&scent=Foaming
I would like the cart to display the following under the scent column when the item is added to the cart:
Foaming Bath Butter 8oz: Mango Papaya, Body Scrub 8oz: Pomegranate, Body Mist 150mL: Bamboo & Teak, Hand Cream 60mL: Unscented
I'm not sure how to pass the data so it will do this. I'm also running into a problem when the scent has a & in the name...
Here is my code for my gift box page:
Code: Select all
<?php
session_start();
require_once("connect.php");
error_reporting(0);
ini_set('display_errors','1');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="./js/functions.js"></script>
<title>$50 Gift Box</title>
<script language=javascript>
// set each of the default scents to "unscented" for each product in this gift basket
var bathScent="Unscented";
var scrubScent="Unscented";
var mistScent="Unscented";
var creamScent="Unscented";
// initialize the variable 'description' that will display the scents selected, in the textarea box like this:
// .................................................
// . Foaming Bath Butter 8oz: Unscented .
// . Body Scrub 8oz: Unscented .
// . Body Mist 150mL: Unscented .
// . Hand Cream 60mL: Unscented .
// .................................................
//////////////////////////////////////////////////////////////
var description="Foaming Bath Butter 8oz: " + bathScent + "\n Body Scrub 8oz: " + scrubScent + "\nBody Mist 150mL: " + mistScent +"\nHand Cream 60mL: " + creamScent;
// function that is called to set the bathScent to whatever scent is selected from the drop down menu under Foaming Bath Butter
function set_bath_scent(x) {
bathScent=x;
// updates the textarea field with the scent selected
updateScents(bathScent);
}
// function that sets the body scrub scent selected from the menu
function set_scrub_scent(x) {
scrubScent=x;
// update body scrub scent
updateScents(x);
}
// sets body mist scent
function set_mist_scent(x) {
mistScent=x;
// update body mist scent
updateScents(mistScent);
}
// set the hand cream scent
function set_cream_scent(x) {
creamScent=x;
// update hand cream scent
updateScents(creamScent);
}
function updateScents(x) {
description="Foaming Bath Butter 8oz: " + bathScent + "\n Body Scrub 8oz: " + scrubScent + "\nBody Mist 150mL: " + mistScent +"\nHand Cream 60mL: " + creamScent;
// display the updated scents selected from the drop down menus
document.getElementById("gift_box_description").value = description;
}
</script>
</head>
<body background="#1d1d1d" onLoad='document.getElementById("gift_box_description").value = description';>
<div id="wrapper">
<div id="main">
<center><img src="./images/50giftBoxLogo.gif"></center>
<table border="0" width="98%" cellspacing=10 cellpadding=5>
<?php
# connect to the data base and put the product scents into an array $n[]
$sql = "SELECT * FROM productscents ORDER BY scentName ASC";
$query = mysql_query($sql);
$n=array();
$prodId;
$i=0;
// put the scent names and IDs into an array
while ( $row = mysql_fetch_assoc($query)) {
$n[$i]=$row['scentName'];
$scent_id[$i]=$row['scentId'];
$i++;
}
$count=count($n);
$count--;
# gets product price
$sql= "SELECT products.productPrice FROM products WHERE products.productName = '$50 Gift Box'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$price=$row['productPrice'];
# gets product id from the products table
$sql2= "SELECT products.productId FROM products WHERE products.productName = '$50 Gift Box'";
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
$prodId=$row2['productId'];
?>
<tr valign=center>
<td><br><br><br>
<table border=1 width=98% cellspacing=5>
<tr valign=top>
<th align=center valign=top >
<font color="#999999">
8oz Foaming Bath Butter
</font>
</th>
<th align=center valign="top">
<font color="#999999">
8oz Body Scrub
</font>
</th>
</tr>
<tr valign=top>
<td align=center valign=top >
<select id="bathButter8oz" class="dropMenu" onChange="set_bath_scent(this.options[selectedIndex].value)"; >
<?php
// add each scent into the dropdown menu for Foaming Bath Butter
echo "<option> -- Select Scent -- </option>";
for ($y=0; $y<=$count;) {
echo "<option value='".$n[$y]."'>".$n[$y]."</option>";
$y++;
}
?>
</select><br><br><br>
</td>
<td align=center valign="top">
<select id="bodyScrub8oz" class="dropMenu" onChange="set_scrub_scent(this.options[selectedIndex].value)"; >
<?php
// add each scent into the dropdown menu for Body Scrub
$menu="<option> --Select Scent-- </option>";
for ($y=0; $y<=$count;) {
$menu.="<option value='".$n[$y]."'>".$n[$y]."</option>";
$y++;
}
echo $menu;
?>
</select><br><br><br>
</td>
</tr>
<tr valign=top>
<th colspan align=center valign=top>
<font color="#999999">
150mL Body Mist
</font>
</th>
<th align=center valign=top>
<font color="#999999">
60mL Hand Cream
</font>
</th>
</tr>
<tr>
<td align=center valign=top>
<select id="bodyMist" class="dropMenu" onChange="set_mist_scent(this.options[selectedIndex].value";>
<?php
// add each scent into the dropdown menu for Body Mist
$menu="<option> --Select Scent-- </option>";
for ($y=0; $y<=$count;) {
$menu.="<option value='".$n[$y]."'>".$n[$y]."</option>";
$y++;
}
echo $menu;
?>
</select><br><br><br>
</td>
<td align=center valign=top>
<select id="handCream60" class="dropMenu" onChange="set_cream_scent(this.options[selectedIndex].value)";>
<?php
// add each scent into the dropdown menu for Hand Cream by looping through the $n[] array
$menu="<option> --Select Scent-- </option>";
for ($y=0; $y<=$count;) {
$menu.="<option value='".$n[$y]."'>".$n[$y]."</option>";
$y++;
}
echo $menu;
?>
</select><br><br><br>
</td>
</tr>
<tr>
<!-- ################### Display box where the scents chosen for each product will be displayed ################# -->
<td colspan=2 align=center valign=center>
<textarea overflow=hidden id="gift_box_description" name="gift_box_description" readOnly=true
style="width: 600px; text-align: center; height: 80px; overflow: hidden; border:1px;
background: #1d1d1d; font-face: arial; font-size: 16px; color: #999999;"></textarea>
</td>
</tr>
<tr>
<td colspan=2 align=center valign=bottom><br>
<!-- Post the javaScript Variable description so the value can be used by PHP -->
<a href=# action=post onClick="window.location.href='giftBox50.php?&scent='+description";>Confirm Scents</a>
<br><br><br>
</td>
</tr>
<!-- table row with the ADD TO CART & VIEW CART links -->
<tr>
<td align="center" valign=center >
<div>
<span>
<form method=post action='cart.php' name='form2'>
<input type="hidden" name="id" id="id" value="<?php echo $prodId;?>"/>
<input type="hidden" name="pid" id="pid" value="<?php echo $prodId;?>"/>
<input type="hidden" name="scent" id="scent" value="<?php print_r( $_GET['scent']);?>"/>
<a href=cart.php?id=<?php echo $prodId?>&pid=<?php echo $prodId;?>&scent=<?php echo $_GET['scent']; ?> ;>Add to Cart</a>
<br>
</form>
</span>
</div>
</td>
<td align=center valign=top>
<a href="cart.php">View Cart</a>
</td>
</tr>
</table></tr>
</table>
</div>
<div id="sidebar">
<?php require ("menu.html"); ?>
</div>
</div> <!-- End wrapper class -->
</body>
</html>