Jump to content

same function in both docs but it does not work in the second doc


demuro1

Recommended Posts

hello. i am working on a term project and it's almost done. I am making a shopping cart and I am working on an add to cart function. I am using an array to capture element id and using that as the index of the array element. I am incrementing the element with every click of the add to cart button. It works in the is simple script but not in the larger one. Can anyone offer any suggestions on how to fix this? Thanks so much This code works:

<!DOCTYPE html><html><head><script>var cart = new Array(); function addToCart(input,form){  var i=parseInt(input.id);  if (typeof cart[i] !== 'undefined' && cart[i] != null)  {cart[i] += 1;}  else {cart[i] = 1;}   alert(input.id + " is in your cart "+cart[i]+' times'); }</script></head><body><table>  <tr>   <td><input type="button" id="0" value="turkey" onclick="addToCart(this);"></td>   <td><input type="button" id="1" value="goose" onclick="addToCart(this);"></td>   <td><input type="button" id="2" value="duck" onclick="addToCart(this);"></td>   </tr></table></body></html>

The trouble section is the add to cart javascript function. It doesn't work here and I just copied and pasted the code.

<html><!--Release Information Release Version 2.0.0Release Notes:		  -page completely rewritten to take advantage of php--><?php#initialize and declare variables#where do I send the form?$formpath = ($_SERVER['PHP_SELF']);#table attributes$border=1;$edgewidth='15%';$tablealignment='centop';$tealign='cenmid';$visibility='hidden';#error reportingerror_reporting(E_ALL);ini_set('display_errors','1');$link = mysqli_connect('127.0.0.1:3306', 'root', '4pp1353c', 'storedatabase');#check connectionif (mysqli_connect_errno()){printf("Connect failed: %s\n", mysqli_connect_error());exit();}#query string and search$query = "SELECT * FROM inventory ORDER by id ASC";$result = mysqli_query($link, $query);$queryReturn = $result->num_rows; #variables$itemsperpage = 3;$itemsperrow = 3;$rowsperpage = $itemsperpage/$itemsperrow; #page number attributes$maxPage = $queryReturn/$itemsperpage;$previousButton;$nextButton;$pageFieldDefaultValue='Jump to Page';#------------------------------------$pageNumber;if(isset($_POST['pageNumber'])){$pageNumber = $_POST['pageNumber'];}else if(!isset($pageNumber)){$pageNumber=1;} #------------------------------------if($pageNumber==1){$previousButton='=X=X=X= Page '.($pageNumber);}else{$previousButton='<==Go to Page '.($pageNumber-1);}if($pageNumber==$maxPage){$nextButton='=X=X=X= Page '.($pageNumber);}else{$nextButton='Go to Page '.($pageNumber+1).'==>';} #items per pageif(!isset($_POST['itemsperpage'])){$itemsperpage=3;}else{$itemsperpage = $_POST['itemsperpage'];}  ?> <head>  <title>Awesome Shape Store</title>   <style>/*sadly this is done without style*/   .centop	{	 text-align:center;	 vertical-align:top;	}   .cenmid	{	 text-align:center;	 vertical-align:middle;	}   .header	{	 font-family:"courier"	 text-align:center;	 vertical-align:top;	 font-size:250%;	 font-weight:bold;	}   .tiny	{	 font-family:"courier"	 text-align:center;	 vertical-align:top;	 font-size:75%;	 font-style:italic;	}  </style>  <script type='text/javascript'>/*make some magic*/ /*-========================================-*//*-- Initialization and Display Functions --*//*-========================================-*/   function onLoad()   {	pageNumber()   }    var timer = setTimer("",0); /* reset the timer for add to cart display or other messages */   function alertDisplay(d)   {	var time=2000; 	document.getElementById("div_display").innerHTML=d;	clearTimeout(timer);	timer=setTimeout("document.getElementById('div_display').innerHTML=''", time);   }/*End Initialize and Display Functions*//*-===================-*//*-- Set hidden info --*//*-===================-*/   function invisibles()   {  	if (document.getElementById('visibles').checked)	{	 document.getElementById('hide1').style.visibility = 'hidden';	 document.getElementById('hide2').style.visibility = 'hidden';	}	else	{	 document.getElementById('hide1').style.visibility = 'visible';	 document.getElementById('hide2').style.visibility = 'visible';	}   }     function itemspp(sel)   {	var items=27; //"<?php echo $queryReturn; ?>";	var itemsperpage = sel.options[sel.selectedIndex].value;	//alert(itemsperpage);  	if (items%itemsperpage != 0)	{document.getElementById('nop').innerHTML = (Math.floor(items/itemsperpage)+1);}	else {document.getElementById('nop').innerHTML = (items/itemsperpage);}  	form.submit();   }/*-====================-*//*-- Action Functions --*//*-====================-*/   //when you add the php to this button id will be named with a php variable and the variable will be the item id   var cart = new Array();   function addToCart(input,form)   {	var i=parseInt(input.id);	if (typeof cart[i] !== 'undefined' && cart[i] != null)	 {cart[i] += 1;}	else {cart[i] = 1;} 	alert(input.id + " is in your cart "+cart[i]+' times');  	var itemName = input.id + "pn"	alertDisplay('A ' + document.getElementById(itemName).innerHTML + ' has been added to your cart');   }     function search(input,form)   {	//alert('You Searched for: ' + document.getElementById('searchField').value);	form.submit();   }     /*==-Page Number Function-==*/   function pageNumber(input,form)   {	//alert('You selected ' + input.id);	var maxPage = <?php echo $maxPage; ?>;	var p = parseInt(document.getElementById('pageNumber').value);  //input.id is numberField  	if ((input.id == 'pageField') &&	(	(isNaN(document.getElementById('pageField').value)) ||	(parseFloat(document.getElementById('pageField').value) % 1 !== 0) ||	(parseInt(document.getElementById('pageField').value) < 1) ||	(parseInt(document.getElementById('pageField').value) > <?php echo $maxPage; ?>)	))	{	 alert('I\'m Sorry, ' + document.getElementById('pageField').value + '\ is not a valid page number. \nYou will be returned to page ' + document.getElementById('pageNumber').value);	}	else if (input.id == 'pageField')	{	 document.getElementById('pageNumber').value = parseInt(document.getElementById('pageField').value);	}//input.id is previousPage	else if (input.id == 'previousPage' && p <= 1)	{	 alertDisplay('You are already on page ' + p);	 return;	}	else if (input.id == 'previousPage')	{	 p -= 1;	 document.getElementById('pageNumber').value = p;	}//input.id is nextPage	else if (input.id == 'nextPage' && p >= <?php echo $maxPage; ?>)	{	 alertDisplay('You are on the last page.');	 return;	}	else if (input.id == 'nextPage')	{	 p += 1;	 document.getElementById('pageNumber').value = p;	}	else {alert('pageNumber Error');}	form.submit();   }   /*-===================================-*//*-- Button and Keystroke Submission --*//*-===================================-*/   function buttonSubmit(button,form)   {	if (button.id == 'searchButton')	{	 search(button,form);	}	else if (button.id == 'previousPage' || button.id == 'nextPage')	{	 pageNumber(button,form);	}	else if (button.name == 'addToCart')	{	 //alert(button.name);	 addToCart(button,form);	}	else	{	 alert('button click error');	 alert(button.id);	 alert(form.name);	}   }   function keystrokeSubmit(textbox,e,form)   {	e = e? e : window.event;	var prskeyrtrn = e.keyCode? e.keyCode : e.which? e.which : e.charCode? e.charCode :null; 	if (prskeyrtrn == 13 && textbox.id == 'searchField')	 {	  search(textbox,form);	 }	else if (prskeyrtrn == 13 && textbox.id == 'pageField')	 {	  pageNumber(textbox,form);	 }	else{}   }  </script></head> <body onLoad="onLoad()"><!--=====================--><!--==begin top of page==--><!--=====================--><form name="headForm" id="headForm"  method="POST" action="<?php echo $formpath; ?>">  <table width="100%" class="<?php echo $tablealignment; ?>" border="<?php echo $border; ?>">   <tr class="<?php echo $tealign; ?>"><!--left side of head -->	<td width="<?php echo $edgewidth; ?>">	 <table height="90" border="<?php echo $border; ?>" class="<?php echo $tablealignment; ?>" width="100%">	  <tr class="<?php echo $tealign; ?>"><td><div name="pagediv" id="pagediv">Current Page: <?php echo $pageNumber; ?></div></td></tr>	  <tr class="<?php echo $tealign; ?>"><td><input type="text" id="pageField" name="pageField" placeholder="<?php echo $pageFieldDefaultValue; ?>" onkeypress="keystrokeSubmit(this,event,form)"></td></tr>	  <tr class="<?php echo $tealign; ?>"><td><input style="cursor:pointer" type="button" id="previousPage" name="previousPage" value="<?php echo $previousButton; ?>" onclick="buttonSubmit(this,form);"></td></tr>	 </table>	</td><!--center of head -->	<td class="<?php echo $tealign; ?>" width="80%">		 <table height="90" border="<?php echo $border; ?>" class="<?php echo $tablealignment; ?>" width="100%">							  <tr class="header"><td height="10%">Awesome Shape Store Catalog</td></tr>							  <tr class="<?php echo $tealign; ?>"><td height="90%"><div id="div_display" class="centop"></div></td></tr>						 </table>					</td><!--right side of head -->	<td width="<?php echo $edgewidth; ?>">	 <table height="90" border="<?php echo $border; ?>" class="<?php echo $tablealignment; ?>" width="100%">	  <tr class="<?php echo $tealign; ?>"><td><input type="text" id="searchField" name="searchField" placeholder="Search" onkeypress="keystrokeSubmit(this,event,form)"></td></tr>	  <tr class="<?php echo $tealign; ?>"><td><input style="cursor:pointer" type="button" id="searchButton" name="searchButton" value="Awesome Search" onclick="buttonSubmit(this,form);"></td></tr>	  <tr class="<?php echo $tealign; ?>"><td><input style="cursor:pointer" type="button" id="nextPage" name="nextPage" value="<?php echo $nextButton; ?>" onclick="buttonSubmit(this,form);"></td></tr>	 </table>	</td>   </tr>  </table><!--This is an invisible table and it holds the pageNumber-->  <table class="tiny" border="<?php echo $border; ?>">   <tr>	<td>	 Page Display Attributes:	</td>	<td colspan="2">	 <input type="checkbox" checked name="visibles" id="visibles" value="show invisibles" onclick="invisibles();">	</td>   </tr>   <tr style="visibility:hidden" name="hideFields" id="hide1">	<td>Current page number</td>	<td>Items per page:</td>	<td>Number of pages:</td>   </tr>   <tr style="visibility:hidden" name="hideFields"id="hide2">	<td><input type="text" id="pageNumber" name="pageNumber" value="<?php echo $pageNumber; ?>"></td>	<td>	 <?php echo $itemsperpage; ?><!--	 <select id="itemsperpage" name="itemsperpage" onchange="itemspp(this);">	  <option value="1">1 item</option>	  <option value="3" selected="selected">3 items</option>	  <option value="6">6 items</option>	  <option value="9">9 items</option>	  <option value="18">18  items</option>	  <option value="27">all items</option>	 </select>-->	</td>	<td><div name="nop" id="nop">9</div></td>   </tr>  </table></form> <!--===================--><!--==end top of page==--><!--===================--> <!--=====	 This is a seperator	 ==========	 This is a seperator	 ==========	 This is a seperator	 =====--><!--=====	 This is a seperator	 ==========	 This is a seperator	 ==========	 This is a seperator	 =====--><!--=====	 This is a seperator	 ==========	 This is a seperator	 ==========	 This is a seperator	 =====--> <!--==============================--><!--==begin main section of page==--><!--==============================--><div name="divbody' id="divbody">   <table>   <?php/*   for ($r=0; $r<=$rowsperpage-1; $r++)   {*/	echo "<tr>";	for ($i=1; $i<=$itemsperrow; $i++)  	{  	 $query = "SELECT * FROM inventory Where id =".($i+($pageNumber-1)*3)." ORDER by id ASC";	 $result = mysqli_query($link, $query);	 $row = mysqli_fetch_assoc($result);		 print	 (	  "<td>	  <table>	  <tr>	  <!--catalog image-->	   <td valign='top'><img src='http://localhost/phpTestCode/web%20apps%20labs/Lab%2010/".$row["imageFilename"]."'></td>	   <td><table>		<tr>		<!--catalog id-->		 <td valign='top'><b>Item ID:</b></td>		 <td>".$row["id"]."</td>		</tr>		<tr>		<!--catalog name-->		 <td valign='top'><b>Item Name:</b></td>		 <td id='".$row['id']."pn'>".$row["Product_name"]."</td>		</tr>		<tr>		<!--catalog shape-->  		 <td valign='top'><b>Shape:</b></td>		 <td>".$row["shape"]."</td>		</tr>		<tr>		<!--catalog size-->		 <td valign='top'><b>Size:</b></td>		 <td>".$row["size"]."</td>		</tr>		<tr>		<!--catalog color-->		 <td valign='top'><b>Color:</b></td>		 <td><font color=".$row['color'].">".$row["color"]."</td>		</tr>		<tr>		<!--catalog price-->		 <td valign='top'><b>Price:</b></td>		 <td>$".$row["price"]."</td>		</tr>		<tr>		<!--catalog description-->		 <td valign='top'><b>Description:</b></td>		 <td> This is an amazing".$row["size"]." <font color=".$row["color"]."><b>".$row["color"]."</b></font> ".$row["shape"]." with a super cool black border</td>		</tr>  	   </table></td>	  </tr>	 <!--add to cart button-->	 <!--id set by php-->	 <!--<form name='addToCart' method='POST'action=". $formpath ."'>-->	  <tr><td><input type='button' value='Add to Cart' name='addToCart' id='".$row["id"]."' onclick='buttonSubmit(this,form);'></td></tr>	 <!--</form>-->		 </table>	 </td>"	 );	}	echo "</tr>";/*   }*/   ?>   </table></div><!--============================--><!--==end main section of page==--><!--============================--></body></html>

Edited by demuro1
Link to comment
Share on other sites

I have confirmed that the correct functions are being called. and nothing happens. The add to cart button may as well not be connected to anything. when I take out the array in the add to cart function suddenly everything works fine but I need the array to work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...