Guys i am trying to create a shopping cart in php + mysql.
I have a page with product and one input button for each one of them.
I want that when ever the product button is clicked it stores the data of product and amount in the database "cart". I tried to call php code in javascript.
Problem1: How i will pass value of input button to php code for processing

here is my code and if you have any simple solution please provide me

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<link rel="stylesheet" type="text/css" href="c.css" />
<title>simple document</title>
<script type="text/javascript">
function cart()
{   
       <?php
            $con=mysql_connect("localhost","root","123") or die("Couldn't open $db: ".mysql_error()); 
            mysql_select_db("shop",$con)or die("Couldn't open $db: ".mysql_error());    
            $product=$_REQUEST['image'];//want button value here
             if($product!="")
             {
                 $id=$_COOKIE['id'];
                 $amount=300;
                 $sql="insert into cart values('$id','$product','$amount')";
                 if (!mysql_query($sql,$con))
                {
                    die('Error: ' . mysql_error());
                }
                else
                    {
                       echo '<META HTTP-EQUIV="Refresh" Content="0; URL=cart.php">';   
                        }
                        }                         
                        mysql_close($con)
             
       ?>
}
</script>
</head>
<body>
<div align="center" class="none" style=" width: 100%;">
<table class="none" width="100%">
<tr>
<td>
<img src="images/main_banner.jpg" height="150"/><br/>
</td> 
<td>
<?php

if(empty($_COOKIE["user"]))
{
echo'
<form name="login" action="login_check.php" method="post">
<table class="none" border="2px solid" style="height: 150px; width: 100px;">
<tr>
<td width="40px">
UserName:
</td>
<td width="40px">
<input class="login" type="text" name="user" />
</td>
</tr>
<tr>
  <td width="40px">
Password:
</td>
<td width="40px">
<input  class="login" type="password" name="pass" />
</td>
</tr>
<tr>
<td>
 <input class="login" value="Login" type="submit" name="login" />
</td> 
<td>  
 <a class="none" style="color:black; font-weight: bold ;" href="reg.php">Register</a>
</td>
</tr>
</table>
</form>'; 
}
else
{
 echo 'Welcome'.$_COOKIE["user"].'<br/>';   
echo '<a href="logout.php">Logout</a>';
}   
?> 
 
<a href="" id="status"></a>
</td>
</td>  
</tr>  
</table>
</div>
 
<br/>
<div align="justify" width="80%">
<table>
<tr>
<td>
<a href="home.php">Home</a>
</td>
<td>
<a href="kids.php">Kids Wear</a>
</td>
<td>
<a href="mens.php">Mens Wear</a>
</td>
<td>
<a href="women.php">Women Wear</a>
</td>
  </tr>
</table>
</div>
 <form method="post" >
<table class="main">
<tr><td><p>Mens Wears</p></td></tr>
<tr>
<td >
<img src="images/m4.jpg" width="150" height="200"/><br/>
</td>
<td >
<img src="images/m2.jpg" width="150" height="200"/><br/>
</td>
<td >
<img src="images/m5.jpg" width="150" height="200"/><br/>
</td>
</tr>
<tr>
<td >
<h5>Rs. 300</h5>
</td>
<td >
<h5>Rs. 300</h5>
</td>
<td >
<h5>Rs. 300</h5>
</td>
</tr>

<tr>
<td >
<input type="image" src="images/ad.jpg" value="m4" style="height: 44px; width: 44px;"> 
</td>
<td >
<input type="image" src="images/ad.jpg" value="m2" style="height: 44px; width: 44px;"> 
</td>
<td >
<input type="image" src="images/ad.jpg" onclick="cart();return false;" value="m5" style="height: 44px; width: 44px;"> 
</td>
</tr>

<tr>
<td >
<img src="images/m1.jpg" width="150" height="200"/><br/>
</td>
<td >
<img src="images/m3.jpg" width="150" height="200"/><br/>
</td>
<td >
<img src="images/m6.jpg" width="150" height="200"/><br/>
</td>
</tr>
<tr>
<td >
<h5>Rs. 300</h5>
</td>
<td >
<h5>Rs. 300</h5>
</td>
<td >
<h5>Rs. 300</h5>
</td>
</tr>
<tr>
<td >
<input type="image" src="images/ad.jpg" value="m1" style="height: 44px; width: 44px;"> 
</td>
<td >
<input type="image" src="images/ad.jpg" value="m3" style="height: 44px; width: 44px;"> 
</td>
<td >
<input type="image" src="images/ad.jpg" value="m5" style="height: 44px; width: 44px;"> // should call the script cart and send its value to it
</td>
</tr>
</table>
</form>
</body>
</html>

one way is to store the id of all the items they have in their cart in array with their session and then when they look at their cart it fetches the prices and adds them. as for calling the code with javascript I do not know.

The simple solution would be to use one of the many existing open-source shopping carts! If you have the skills and experience to write your own and there is a good reason for doing it, then go for it. If that was the case, then I don't think that you would have had to add this post.

You have PHP code wrapped in a Javascript function and that doesn't make sense to me. The PHP needs to be resolved on the server before it is sent to the Browser. If you have variables that you are determining on the server, then you can insert those php variables into the Javascript code and they will be resolved and sent to the Browser as part of the Javascript. You can't interactively call PHP from Javascript unless you are using Ajax but that is whole different thing. You have onlick="cart()... and this will happen in the Browser but what will the cart function contain at that point? What will the server have generated and sent to the Browser (in html and Javascript)? Probably nothing! If you can make this code work, then more power to you but I don't think that is going to happen.

In straight PHP, you need to click on a form (Submit) button to send form data to the processing module (which can be the same module but you need code to determine what phase of processing it is in). That module will handle the MySQL updating. If you want to use onclick and javascript or some Ajax instead, then you are into a somewhat complicated approach and you probably need to spend some time on tutorials before you will be ready to tackle it properly. If it was me, I'd find a good open-source cart and use it. If I had special requirements, I'd try to customize an existing cart (which I have done before myself) rather than try to build one from scratch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.