swiltch
03-07-2012, 10:04 AM
Hello, I'm hoping someone can help me with this.
I have 3 pages that I need to put into my framework, they are products, shopping cart and billing. At the moment they work perfectly fine. Here is a live example -
http://www.cems.uwe.ac.uk/~r4-george/wp2/shopping/products.php
Now, I have a framework for a whole website that I need to put these pages into. (http://www.cems.uwe.ac.uk/~r4-george/wp4/index.php) The index uses a switch statement to go between pages.
Here is the index.php
<?php # index.php
/*
* This is the main page.
* This page includes the configuration file,
* the templates, and any content-specific modules.
*/
// Require the configuration file before any PHP code:
require_once ('./modules/config.inc.php');
// Validate what page to show:
if (isset($_GET['p'])) {
$p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
$p = $_POST['p'];
} else {
$p = NULL;
}
// Determine what page to display:
switch ($p) {
case 'about':
$page = 'about.inc.php';
$page_title = 'About This Site Again';
break;
case 'products':
$page = 'products.inc.php';
$page_title = 'Products on this site';
break;
case 'this':
$page = 'this.inc.php';
$page_title = 'This is Another Page.';
break;
case 'that':
$page = 'that.inc.php';
$page_title = 'That is Also a Page.';
break;
case 'contact':
$page = 'contact.inc.php';
$page_title = 'Contact Us';
break;
case 'search':
$page = 'search.inc.php';
$page_title = 'Search Results';
break;
// Default is to include the main page.
default:
$page = 'main.inc.php';
$page_title = 'Site Home Page';
break;
} // End of main switch.
// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
$page = 'main.inc.php';
$page_title = 'Site Home Page';
}
// Include the header file:
include_once ('./includes/header.inc');
echo "<div id=\"content\">";
// Include the content-specific module:
// $page is determined from the above switch.
include ('./modules/' . $page);
// Include the footer file to complete the template:
include_once ('./includes/footer.inc');
?>
It uses the .inc.php files located in the modules folder to switch between pages.
Here is my products.inc.php -
<?
include("includes/db.php");
include("includes/functions.php");
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Products</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
</form>
<div align="center">
<h1 align="center">Products</h1>
<table border="0" cellpadding="2px" width="600px">
<?
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><img src="<?=$row['picture']?>" /></td>
<td> <b><?=$row['name']?></b><br />
<?=$row['description']?><br />
Price:<big style="color:green">
£<?=$row['price']?></big><br /><br />
<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>
</table>
</div>
</body>
</html>
This is EXACTLY the same code as the working example. The products get listed correctly but the problem I have is the 'Add to Cart' button fails to work.
Live example - http://www.cems.uwe.ac.uk/~r4-george/wp4/index.php?p=products
Everything is in the right directory. When I inspect the 'Add to Cart' button in chrome I get the following -
Uncaught TypeError: Cannot set property 'value' of undefined
addtocart index.php:51
(anonymous function)index.php:83
onclick
Any help is really appreciated, I'm struggling to see what I have done wrong. I don't know whether it's a Javascript problem.
If you need any of the code from other pages I can post it too.
Thanks in advance.
I have 3 pages that I need to put into my framework, they are products, shopping cart and billing. At the moment they work perfectly fine. Here is a live example -
http://www.cems.uwe.ac.uk/~r4-george/wp2/shopping/products.php
Now, I have a framework for a whole website that I need to put these pages into. (http://www.cems.uwe.ac.uk/~r4-george/wp4/index.php) The index uses a switch statement to go between pages.
Here is the index.php
<?php # index.php
/*
* This is the main page.
* This page includes the configuration file,
* the templates, and any content-specific modules.
*/
// Require the configuration file before any PHP code:
require_once ('./modules/config.inc.php');
// Validate what page to show:
if (isset($_GET['p'])) {
$p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
$p = $_POST['p'];
} else {
$p = NULL;
}
// Determine what page to display:
switch ($p) {
case 'about':
$page = 'about.inc.php';
$page_title = 'About This Site Again';
break;
case 'products':
$page = 'products.inc.php';
$page_title = 'Products on this site';
break;
case 'this':
$page = 'this.inc.php';
$page_title = 'This is Another Page.';
break;
case 'that':
$page = 'that.inc.php';
$page_title = 'That is Also a Page.';
break;
case 'contact':
$page = 'contact.inc.php';
$page_title = 'Contact Us';
break;
case 'search':
$page = 'search.inc.php';
$page_title = 'Search Results';
break;
// Default is to include the main page.
default:
$page = 'main.inc.php';
$page_title = 'Site Home Page';
break;
} // End of main switch.
// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
$page = 'main.inc.php';
$page_title = 'Site Home Page';
}
// Include the header file:
include_once ('./includes/header.inc');
echo "<div id=\"content\">";
// Include the content-specific module:
// $page is determined from the above switch.
include ('./modules/' . $page);
// Include the footer file to complete the template:
include_once ('./includes/footer.inc');
?>
It uses the .inc.php files located in the modules folder to switch between pages.
Here is my products.inc.php -
<?
include("includes/db.php");
include("includes/functions.php");
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:shoppingcart.php");
exit();
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Products</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
</form>
<div align="center">
<h1 align="center">Products</h1>
<table border="0" cellpadding="2px" width="600px">
<?
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><img src="<?=$row['picture']?>" /></td>
<td> <b><?=$row['name']?></b><br />
<?=$row['description']?><br />
Price:<big style="color:green">
£<?=$row['price']?></big><br /><br />
<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<? } ?>
</table>
</div>
</body>
</html>
This is EXACTLY the same code as the working example. The products get listed correctly but the problem I have is the 'Add to Cart' button fails to work.
Live example - http://www.cems.uwe.ac.uk/~r4-george/wp4/index.php?p=products
Everything is in the right directory. When I inspect the 'Add to Cart' button in chrome I get the following -
Uncaught TypeError: Cannot set property 'value' of undefined
addtocart index.php:51
(anonymous function)index.php:83
onclick
Any help is really appreciated, I'm struggling to see what I have done wrong. I don't know whether it's a Javascript problem.
If you need any of the code from other pages I can post it too.
Thanks in advance.