Jump to content

Recommended Posts

Hello.

 

First of all, this is my first post here so I'm not sure if this is the right place, hopefully it is :)

And my english is not perfect, so I hope you will understand everything.

 

I'm kind of new to php, but I do have experience with php and webprogramming.

So the problem is that I'm not sure what exactly I need. The closest comparison I could make is with a shopping cart.

 

I have a website were people can select a package to make a offer/tender. For example, there are 2 pages, one with drinks and one with food and on each page they can select a couple of options. With the list of items they have selected in the top right of the page (that would be the shopping cart) so they can see what they have selected. And at the end, they can click on "send" so I can make a price list, instead of checkout (what they would do in a webshop).

 

I'm not very experienced so maybe a pre-made solution would do the thing, or a site which has some scripts I could use.

What I thought of was to use a database but I guess that would be really messy afterall..

I've looked in w3schools and I found a couple of things like cookies and sessions but the explanation they provided was not enough for me to do something with it.

 

I hope someone here could help me or has the same problem!

 

Thanks alot,

-Colin

Edited by colinch
Link to comment
https://forums.phpfreaks.com/topic/270904-need-help-with-shopcart/
Share on other sites

1. Do not use w3Schools as a resource. They use outdated and flat out wrong coding procedures.

2. Have you tried googleing something like "PHP shopping cart tutorial"? This should link you to a number of resources on the subject.

Thanks for the reply! I've found some codes for a the system I need.

But to make it work the way I need it to work, I need the page: shoppingcart.php to actually be visible on my product pages, so people will be able to see what they've selected on the page they are currently on.

From what I now, you can only include a php file in a html file, so not php in php or the html of a php in another php?

I'm having this problem for a long time and also googled it, but I can't find a solution for it, hopefully someone here can help me!

Ok thanks, I have tried something now.

This is the normal system i downloaded: http://www.colinch.com/11events

When I add a product, it takes me to the shoppingcart page.

What I want to do, is display the shoppingcart on the product page (because I want to make more than 1 product pages).

I did it like this: http://www.colinch.com/11events/products2.php

 

But, for some reason I can't remove or add when the shopping cart is there (I just copied the code of shoppingcart.php in products.php which results in products2.php)

 

Here is the code:

 

<?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>




<?


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';
  }
 }
}


?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<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>


<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<div style="margin:0px auto; width:600px;" >
   <div style="padding-bottom:10px">
    <h1 align="center">Your Shopping Cart</h1>
   <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
   </div>
    <div style="color:#F00"><?=$msg?></div>
    <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
    <?php
  if(is_array($_SESSION['cart'])){
            echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
   $max=count($_SESSION['cart']);
   for($i=0;$i<$max;$i++){
 $pid=$_SESSION['cart'][$i]['productid'];
 $q=$_SESSION['cart'][$i]['qty'];
 $pname=get_product_name($pid);
 if($q==0) continue;
  ?>
             <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
                   <td>$ <?=get_price($pid)?></td>
                   <td><input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
                   <td>$ <?=get_price($pid)*$q?></td>
                   <td><a href="javascript:del(<?=$pid?>)">Remove</a></td></tr>
           <?	
   }
  ?>
   <tr><td><b>Order Total: $<?=get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr>
  <?
           }
  else{
   echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
  }
 ?>
       </table>
   </div>
</form>




<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">
 <?php
  $result=mysql_query("select * from products");
  while($row=mysql_fetch_array($result)){
 ?>
    <tr>
        <td><img src="<?php echo $row['picture']?>" /></td>
           <td>    <b><?php echo $row['name']?></b><br />
             <?php echo $row['description']?><br />
                   Price:<big style="color:green">
                    $<?php echo $row['price']?></big><br /><br />
                   <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" />
  </td>
 </tr>
       <tr><td colspan="2"><hr size="1" /></td>
       <?php } ?>
   </table>
</div>
</body>
</html>

 

I hope this can be solved because I think a lot of people could use this when it works properly!

let me hope you want to have a module or call it a plugin in your script that desplay the shopping cart and has a checkout batton. it updates everytime a user picks somthing and after selecting all that they want then they can either go to the page that you have now or they checkout.

 

is that what you want?

On the products page, you are receiving a wrapper error. Enable the allow_url_include directive in the master php.ini file.

Using developer tools, a front end error is triggered when the "Add to Cart" button is clicked:

 

Uncaught TypeError: Cannot set property 'value' of undefined

 

I believe this is because when you add the shopingcart.php code to products.php, you have two forms with the same name "form1". In order for the js code to work properly, these forms should have unique names.

Edited by AyKay47

@50r

Yeah that's kind of what I need, but I think i'm almost done :)

 

@AyKay47

Thanks, that did the trick!

So, at www.colinch.com/11events/products2.php I can add products, and modify the cart.

But, when someone adds products, it will take them to the shoppingcart.php

And what I want to do, is remove the shoppingcart.php completely and make the cart only available at the product pages.

 

My (hopefully) last question is, how could I make it so when I add a product to the cart it won't take me to another page but when clicked on the add button it will either:

-refresh the page

-adds a new row to the shopping cart without refreshing (like it does when you remove a product).

 

Thanks in advance for the replies so far!

 

-Colin

Edited by colinch
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.