troubleshooting Question

How do I add variables in javascript and php?

Avatar of sofuscomer
sofuscomerFlag for Denmark asked on
JavaScriptPHPMySQL Server
10 Comments2 Solutions814 ViewsLast Modified:
Dear folks
I have downloaded a very nice and easy tutorial from a guy called Richard Clark.
http://www.qualitycodes.com/tutorial.php?articleid=25&title=Tutorial-Building-a-shopping-cart-in-PHP

I got it all up and running on my site: http://www.sofuscomer.com/shopping

Its all working. But I wish to add a select field on the products.php page that shows a dropdown of choices, that may change the price of the image added to the cart. This is not included in the tutorial, so I have tried to include it myself. Please look at the bold code, cause this is where I think I have to change some code or add some lines to make it add the selected priceid from the select dropdown. I am really new at javascript and $_SESSION coding.

Below is the current code (Modified paths and mysql tables for security).
Below that is the code for the functions.php
And finally the code for the shoppingcart.php

products.php

 <?php
include_once "path/db.php";
include_once "path/functions.php";

[b]if ($_REQUEST['command']=='add' && $_REQUEST['productid']>0) {
	$pid=$_REQUEST['productid'];
	addtocart($pid, 1);
	header("location:shoppingcart.php");
	exit();
}
$priceid=$REQUEST['priceid'];[/b]
?>
<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
    <title>Photojournalist and webdesigner Sofus Comer - Thy, Denmark</title>
	<link href="../stylesheet.css" rel="stylesheet" type="text/css" media="only screen and (min-width:961px)" />
	<!-- iphone -->
	<link href="../iphone.css" rel="stylesheet" type="text/css" media="only screen and (max-width:960px)">
	[b]<script language="javascript">
	function addtocart(pid,priceid){
		document.form1.productid.value=pid;
		document.form1.priceid.value=priceid;
		document.form1.command.value='add';
		document.form1.submit();
	}
	</script>[/b]
</head>
<body>
<div class="div_master">
<div class="titlediv">Photojournalist Sofus Comer</div>
<?php
include_once "../menu_includes/menu_main.php";
?>
<div class="descript"></div>
<div class="div_main">
<div class="page_right">
    <div class="portfolio_view_title">Ordering Prints</div>
    <div class="photostreamExif"></div>
    <div class="clear"></div>
    </div>
    <div class="page_master">
<form name="form1">
	<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
	<table border="0" cellpadding="2px" width="auto">
		<?php
$result=mysql_query("select * from sometable WHERE ids = 630 ORDER BY ids DESC LIMIT 100");
while ($row=mysql_fetch_array($result)) {
?>
    	<tr>
        	<td><img src="../path_to_images/somemore_<?php echo $row['idf']?>.jpg" class="img_border_white"/></td>
            <td><b>Image ID: <?php echo $row['ids'];?>_<?php echo $row['idf'];?>_<?php echo $row['image']?></b>
            <br />
            <?php echo $row['caption']?>
            <br />

                    	Price:<big style="color:green">
       [b] <select name="priceid" id="priceid">
				<option value="1">A4 (With 1,2cm white border)</option>
				<option value="2">A3 (With 1,2cm white border)</option>
				<option value="3">A3+ (With 1,2cm white border)</option>
				<option value="4">A2 (With 1,2cm white border)</option>
				<option value="5">A2 (Borderless)</option>
		</select>[/b]
		</form>
		</big>

                    	<br /><br />
                    <input type="button" value="Add to cart" onclick="addtocart(<?php echo $row['idf']?>)" />
			</td>
		</tr>
        <tr><td colspan="2"><hr size="1" /></td>
        <?php } ?>
    </table>
</div>
</div>
</div>
</div>
</body>
</html>

functions.php

<?php
	function get_product_name($pid){
		$result=mysql_query("select idf,image,ids from tablename where idf=$pid");
		$row=mysql_fetch_array($result);
		return $row['ids']."_".$row['idf']."_".$row['image'];
	}
	[b]function get_price($priceid){
		$result=mysql_query("select price from printpricesandoptions where id=$priceid");
		$row=mysql_fetch_array($result);
		return $row['price'];
	}[/b]
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
	function get_order_total(){
		$max=count($_SESSION['cart']);
		$sum=0;
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=$_SESSION['cart'][$i]['qty'];
			$price=get_price($priceid);
			$sum+=$price*$q;
		}
		return $sum;
	}
	function addtocart($pid,$q){
		if($pid<1 or $q<1) return;
		
		if(is_array($_SESSION['cart'])){
			if(product_exists($pid)) return;
			$max=count($_SESSION['cart']);
			$_SESSION['cart'][$max]['productid']=$pid;
			$_SESSION['cart'][$max]['priceid']=$price;
			$_SESSION['cart'][$max]['qty']=$q;
		}
		else{
			$_SESSION['cart']=array();
			$_SESSION['cart'][0]['productid']=$pid;
			$_SESSION['cart'][0]['priceid']=$price;
			$_SESSION['cart'][0]['qty']=$q;
		}
	}
	function product_exists($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=0;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$flag=1;
				break;
			}
		}
		return $flag;
	}

?>


shoppingscart.php

<?php
	include("path/db.php");
	include("path/functions.php");


	if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
		remove_product($_REQUEST['pid']);
	}
	else if($_REQUEST['command']=='clear'){
		unset($_SESSION['cart']);
	}
	else if($_REQUEST['command']=='update'){
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=intval($_REQUEST['product'.$pid]);
			if($q>0 && $q<=999){
				$_SESSION['cart'][$i]['qty']=$q;
			}
			else{
				$msg='Some products not updated!, quantity must be a number between 1 and 999';
			}
		}
	}

?>

And in the <head>
<script language="javascript">
	function del(pid){
		if(confirm('Do you really mean to delete this item')){
			document.form1.pid.value=pid;
			document.form1.command.value='delete';
			document.form1.submit();
		}
	}
	function clear_cart(){
		if(confirm('This will empty your shopping cart, continue?')){
			document.form1.command.value='clear';
			document.form1.submit();
		}
	}
	function update_cart(){
		document.form1.command.value='update';
		document.form1.submit();
	}


</script>

Thanks a lot anybody who can assist me here.
ASKER CERTIFIED SOLUTION
Mark Brady
Principal Data Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros