Reputation: 45
So I am trying to make a shopping cart. What I need to do is send a product id and a quantity the user wants to buy to another file cart.php
.
Here is a section of my code:
for($i=0;$i<$numOfRows;$i++){
$prodID=mysql_result($result, $i, "ProductID");
$prodTitle=mysql_result($result, $i, "Title");
$prodAuthor=mysql_result($result, $i, "Author1");
$prodPrice=mysql_result($result, $i, "Price");
Print"<h4>ID: $prodID \n </h4>";
Print"<h4>Title: $prodTitle \n </h4>";
Print"<h4>Author: $prodAuthor \n </h4>";
Print"<h4>Price: $ $prodPrice \n </h4>";
Print" <form method ='POST'>";
Print"<p><label> Quantity";
Print"<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
Print"</label>";
Print"<input type='button' value='Add To Cart' onClick='cart.php?pid=$prodID'/>";
Print"</p></form>";
}
If I get rid of the form and just have <a href='cart.php?pid=$prodID'>Add To Cart</a>
it sends the id but I also need the quantity to go along with it.
Whenever I click Add To Cart it doesn’t send me to my cart.php
.
Upvotes: 0
Views: 71
Reputation: 41885
Why not just let it submit the form normally. And use a normal type="submit"
with method="GET"
:
echo "<form method ='GET' action='cart.php'>";
echo "<p><label> Quantity: ";
echo "<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
echo "</label>";
echo "
<input type='hidden' name='pid' value='$prodID' />
<input type='submit' value='Add To Cart' />
";
echo "</p></form>";
Of course in cart.php
, just use $_GET
all through out:
<?php
if(!empty($_GET['quantity']) && !empty($_GET['pid'])) {
$quantity = $_GET['quantity'];
$pid = $_GET['pid'];
// do the rest of codes below
}
Obligatory Note:
Please, don't use
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Upvotes: 0
Reputation: 790
Your onlick is doing a method GET while your form indicates a method POST and you're not specifying "action='cart.php'" in your form tag... there may be other issue but...
try this
for($i=0;$i<$numOfRows;$i++)
{
$prodID=mysql_result($result, $i, "ProductID");
$prodTitle=mysql_result($result, $i, "Title");
$prodAuthor=mysql_result($result, $i, "Author1");
$prodPrice=mysql_result($result, $i, "Price");
Print"<h4>ID: $prodID \n </h4>";
Print"<h4>Title: $prodTitle \n </h4>";
Print"<h4>Author: $prodAuthor \n </h4>";
Print"<h4>Price: $ $prodPrice \n </h4>";
Print"<form action='cart.php' method ='POST'>";
Print"<p><label> Quantity";
Print"<input name='pid' type = 'hidden' value='$prodID'>";
Print"<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
Print"</label>";
Print"<input type='submit' value='Add To Cart'>";
Print"</p></form>";
}
Upvotes: 0
Reputation: 7791
If you want use the POST method add a hidden input with the pid info. See the example:
print" <form method ='POST' action ='cart.php'>";
print"<p><label> Quantity";
print"<input name='quantity' type='text' pattern='[1-9]{1,50}'/>";
print"</label>";
print"<input type='hidden' name = 'pid' value='$prodID'>";
print"<input type='button' value='Add To Cart'/>";
print"</p></form>";
If you want use the GET method see the @Ghost answer
Upvotes: 1