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>