|
-
April 20th, 2008, 05:20 PM
#1
Need help with Javascript & PHP
This php file runs just fine on my computer; however, when I put it onto my server, it doesn't. The "Add to cart" and "View Cart" buttons no longer work. Basically, I'm using cookies to keep track of the items inside a shopping cart.
k001 is one of the products. The other product pages are not updated. This is a school project. You can better understand my problem by visiting the website below. If you copy the code and run it yourself in a web browser, it should function properly. Thanks!
Code:
cs-sun2000.uscupstate.edu/~student12/k001.php
Code:
<?php
//removed mysql connect for this post - not needed
$query = "SELECT * FROM products WHERE id = '3001'";
$result = mysql_query($query);
$id = mysql_result($result,0,"id");
$image = mysql_result($result,0,"image");
$title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
$price = mysql_result($result,0,"price");
mysql_close();
$file_handle = fopen("template.data", r);
while(!feof($file_handle)) {
$line = fgets($file_handle);
if(preg_match("/@@CONTENT HERE@@/", $line, $result)){
echo "<table border=0 width=760 align=center cellpadding=0 cellspacing=0>
<tr>
<td colspan=4 width=760 height=60>
</td>
</tr> <!-- End of row -->
<tr>
<td width=40 height=200>
</td>
<td width=330 height=200 valign=top align=center>
<img src='images/kids/".$image.".gif' title=$title>
</td>
<td width=330 height=200 valign=top align=left>
<b>$title</b>
</br></br>
<pp>".$description."
</br></br>
<b>-- Only $price</b></pp>
</br></br>
</td>
<td width=60 height=200>
</td>
</tr> <!-- End of row -->
<tr>
<td colspan=2></td>
<td width=270 valign=center align=left>
<form method='post' name='form'>
<input name='id' type='hidden' value='k001'>
<input name='process_cart' type='button' value='Add to cart' onclick='processCart()'>
<input name='subbox' type='button' value='-'' onclick='subitem()''>
<input name='quantity' type='text' value='0' size=2>
<input name='addbox' type='button' value='+' onclick='additem()''>
<input name='view_cart' type='button' value='View Cart' onclick='viewCart()'>
</form>
</td>
<td width=60></td>
</tr> <!-- End of row -->
</table>";
print<<<END
<!-- HTML HERE -->
<script language="JavaScript">
<!--
function SymError() {
return true;
}
window.onerror = SymError;
var SymRealWinOpen = window.open;
function SymWinOpen(url, name, attributes) {
return (new Object());
}
window.open = SymWinOpen;
-->
</script>
<script language="JavaScript">
<!-- //This will add item to cart
function processCart() {
var id = document.form.id.value;
var new_data = document.form.quantity.value;
var cookieData;
var cookieData2;
if(new_data > -1) {
if(document.cookie && document.cookie != "") {
cookieData = unescape(document.cookie);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(items[0] == id) {
var newnum = (eval(items[1]) + eval(new_data));
cookieData = cookieData.replace(id + ":" + items[1],id + ":" + newnum);
document.cookie = escape(cookieData);
}}}
else {
cookieData = "cart=;";
cookieData = cookieData + "k001:0-k002:0-k003:0-m001:0-m002:0-m003:0-m004:0-m005:0-m006:0-w001:0-w002:0-w003:0-w004:0-w005:0-w006:0";
document.cookie = escape(cookieData);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(items[0] == id) {
var newnum = (eval(items[1]) + eval(new_data));
cookieData2 = cookieData.replace(id + ":" + items[1],id + ":" + newnum);
document.cookie = escape(cookieData2);
}}}}
else {
alert("Please enter a number 0 or greater");
}}
-->
</script>
<script language="JavaScript">
<!-- //This will alert cart status:
function viewCart() {
var cartValue = "";
var header = "Shopping Cart Contents:\n\n";
if(document.cookie && document.cookie != "") {
var cookieData = unescape(document.cookie);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(eval(items[1])>0){
cartValue = cartValue + "Product Number: " + items[0] + "\nQuantity: " + items[1] + "\n\n";
}}
if(cartValue != "") {
alert(header + cartValue);
}}
else {
alert("Empty Cart");
}}
-->
</script>
<script language="JavaScript">
<!-- //This will reset cookie back to zero
function kill_cookie() {
document.cookie = "";
}
-->
</script>
<script language="JavaScript">
<!-- //This will add the [<][ ][>] field
function additem() {
var x = document.form.quantity.value;
if(parseInt(x) <= 99) {
document.form.quantity.value = parseInt(x) + 1;
}}
function subitem() {
var x = document.form.quantity.value;
if(parseInt(x) > 0) {
document.form.quantity.value = parseInt(x) - 1;
}
else {
document.form.quantity.value = 0;
}}
-->
</script>
<!-- HTML HERE -->
END;
}else{
print $line;
}
}
?>
-
April 21st, 2008, 04:19 PM
#2
Re: Need help with Javascript & PHP
 Originally Posted by rdjonesku
This php file runs just fine on my computer; however, when I put it onto my server, it doesn't. The "Add to cart" and "View Cart" buttons no longer work. Basically, I'm using cookies to keep track of the items inside a shopping cart.
k001 is one of the products. The other product pages are not updated. This is a school project. You can better understand my problem by visiting the website below. If you copy the code and run it yourself in a web browser, it should function properly. Thanks!
Code:
cs-sun2000.uscupstate.edu/~student12/k001.php
Code:
<?php
//removed mysql connect for this post - not needed
$query = "SELECT * FROM products WHERE id = '3001'";
$result = mysql_query($query);
$id = mysql_result($result,0,"id");
$image = mysql_result($result,0,"image");
$title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
$price = mysql_result($result,0,"price");
mysql_close();
$file_handle = fopen("template.data", r);
while(!feof($file_handle)) {
$line = fgets($file_handle);
if(preg_match("/@@CONTENT HERE@@/", $line, $result)){
echo "<table border=0 width=760 align=center cellpadding=0 cellspacing=0>
<tr>
<td colspan=4 width=760 height=60>
</td>
</tr> <!-- End of row -->
<tr>
<td width=40 height=200>
</td>
<td width=330 height=200 valign=top align=center>
<img src='images/kids/".$image.".gif' title=$title>
</td>
<td width=330 height=200 valign=top align=left>
<b>$title</b>
</br></br>
<pp>".$description."
</br></br>
<b>-- Only $price</b></pp>
</br></br>
</td>
<td width=60 height=200>
</td>
</tr> <!-- End of row -->
<tr>
<td colspan=2></td>
<td width=270 valign=center align=left>
<form method='post' name='form'>
<input name='id' type='hidden' value='k001'>
<input name='process_cart' type='button' value='Add to cart' onclick='processCart()'>
<input name='subbox' type='button' value='-'' onclick='subitem()''>
<input name='quantity' type='text' value='0' size=2>
<input name='addbox' type='button' value='+' onclick='additem()''>
<input name='view_cart' type='button' value='View Cart' onclick='viewCart()'>
</form>
</td>
<td width=60></td>
</tr> <!-- End of row -->
</table>";
print<<<END
<!-- HTML HERE -->
<script language="JavaScript">
<!--
function SymError() {
return true;
}
window.onerror = SymError;
var SymRealWinOpen = window.open;
function SymWinOpen(url, name, attributes) {
return (new Object());
}
window.open = SymWinOpen;
-->
</script>
<script language="JavaScript">
<!-- //This will add item to cart
function processCart() {
var id = document.form.id.value;
var new_data = document.form.quantity.value;
var cookieData;
var cookieData2;
if(new_data > -1) {
if(document.cookie && document.cookie != "") {
cookieData = unescape(document.cookie);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(items[0] == id) {
var newnum = (eval(items[1]) + eval(new_data));
cookieData = cookieData.replace(id + ":" + items[1],id + ":" + newnum);
document.cookie = escape(cookieData);
}}}
else {
cookieData = "cart=;";
cookieData = cookieData + "k001:0-k002:0-k003:0-m001:0-m002:0-m003:0-m004:0-m005:0-m006:0-w001:0-w002:0-w003:0-w004:0-w005:0-w006:0";
document.cookie = escape(cookieData);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(items[0] == id) {
var newnum = (eval(items[1]) + eval(new_data));
cookieData2 = cookieData.replace(id + ":" + items[1],id + ":" + newnum);
document.cookie = escape(cookieData2);
}}}}
else {
alert("Please enter a number 0 or greater");
}}
-->
</script>
<script language="JavaScript">
<!-- //This will alert cart status:
function viewCart() {
var cartValue = "";
var header = "Shopping Cart Contents:\n\n";
if(document.cookie && document.cookie != "") {
var cookieData = unescape(document.cookie);
var crumbs = cookieData.split(";");
var pieces = crumbs[1].split("-");
for(i=0;i<pieces.length;++i) {
var items = pieces[i].split(":");
if(eval(items[1])>0){
cartValue = cartValue + "Product Number: " + items[0] + "\nQuantity: " + items[1] + "\n\n";
}}
if(cartValue != "") {
alert(header + cartValue);
}}
else {
alert("Empty Cart");
}}
-->
</script>
<script language="JavaScript">
<!-- //This will reset cookie back to zero
function kill_cookie() {
document.cookie = "";
}
-->
</script>
<script language="JavaScript">
<!-- //This will add the [<][ ][>] field
function additem() {
var x = document.form.quantity.value;
if(parseInt(x) <= 99) {
document.form.quantity.value = parseInt(x) + 1;
}}
function subitem() {
var x = document.form.quantity.value;
if(parseInt(x) > 0) {
document.form.quantity.value = parseInt(x) - 1;
}
else {
document.form.quantity.value = 0;
}}
-->
</script>
<!-- HTML HERE -->
END;
}else{
print $line;
}
}
?>
This is not the way to code a shopping cart application. Move your cart functions over to the PHP script and use a PHP session to keep track of the products etc.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|