-2

I have 2 pages -> product.php and cart.php .

when user clicks on "add to cart" button on product.php , it will reach at cart.php. However, my quantity is not adde successfully no mater how many times i clicked. Quantity always remain 1.

product.php

echo '<form action="cart.php" method="POST">';
echo '<input type="hidden" name="quantity" value="0">';
echo '<input type="submit" value="add to cart">';
echo '</form>';

cart.php

if (isset($_POST["quantity"])){

$_POST["quantity"]=$_POST["quantity"]+1;
}
echo $_POST["quantity"];

May I know which part has gone wrong?

gosulove
  • 1,645
  • 2
  • 26
  • 40
  • remove `value="0"` from `echo '';` and why this is hidden input?how you are filling quantity ? – Alive to die - Anant Aug 14 '16 at 02:58
  • I testing the add to cart function and see if the quantity is added successfully or not. That's why i use hidden for input. Even i remove value="0", it remains the same. – gosulove Aug 14 '16 at 03:02
  • "add to cart" usually involves sessions; are you using them? Btw, your echo should go in the conditional statement. Your code works. – Funk Forty Niner Aug 14 '16 at 03:07
  • 2
    *"However, my quantity is not adde successfully no mater how many times i clicked. Quantity always remain 1."* - Because that's just what your code does. If you're looking to increment, you need to use sessions. – Funk Forty Niner Aug 14 '16 at 03:09
  • I tried session in this way, but it still the same – gosulove Aug 14 '16 at 03:21
  • @gosulove I have posted two examples below. One if used in the same file, and another if used in two separate files; they are not the same. – Funk Forty Niner Aug 14 '16 at 13:03

2 Answers2

1

As @Fred -ii said would be good if you use session for this functionality follows if you need help to make you can see: shopping cart Session php

Also consider do in javascript follows an interesting plugin for shopping carts in js: http://www.simplecartjs.org or http://www.jqueryrain.com/?ZpwnOCVU

Community
  • 1
  • 1
charlan alves
  • 384
  • 3
  • 17
1

As I mentioned in comments, you need to use sessions for this in order to increment a number each time the button for it is clicked.

I have to admit that I pulled/borrowed the following from this answer (which I upvoted I might add).

N.B: A second version of the following exists just below it.

If used in the same file:

<?php 

session_start();

if(isset($_POST['reset'])){ 

   unset($_SESSION['number']);

   session_destroy();

$_SESSION['number']=0;

}

if(!isset($_SESSION['number'])){
    $_SESSION['number']=1;
}elseif(isset($_POST['next'])){

    $_SESSION['number']++;

}

echo '
<form action="" method="POST">
   <input class="big_b" type="submit" name="next" value="Add to cart" /> 
   <input type="submit" name="reset" value="Reset" /> 
</form>';

echo $_SESSION['number'];

If used in two files:

File 1:

<?php 

echo '
<form action="next_page.php" method="POST">
   <input class="big_b" type="submit" name="next" value="Add to cart" /> 
   <input type="submit" name="reset" value="Reset" /> 
</form>';

File 2: (next_page.php)

<?php 

session_start();

// Reset to 1
if(isset($_POST['reset'])){ 

   unset($_SESSION['number']); // unset the session

   session_destroy(); // make sure the session is destroyed

$_SESSION['number']=0; // reset it back to zero

}

if(!isset($_SESSION['number'])){
    $_SESSION['number']=1;
}elseif(isset($_POST['next'])){
    $_SESSION['number']++;

}

echo $_SESSION['number'];

Reference:


How to check if a session is equal to a certain number:

You can also check if the session is set and is equal to a certain number:

if(isset($_SESSION['number']) && $_SESSION['number'] == 5) {

echo "You have reached 5. The session has been reset back to zero.";

   unset($_SESSION['number']);

   session_destroy();

$_SESSION['number']=0;

}
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Hi Fred , thx for ur codes. It works now. However, just want to ask you this questions; If we press F5 and refresh it, the number will increase by 1. Is there a way to prevent such thing? – gosulove Aug 15 '16 at 05:02
  • @gosulove You can use a header to redirect http://php.net/manual/en/function.header.php but you can't "echo" at the same time. If the header does not work, then you may be outputting before header as it is called. You may need to redirect using other methods that are outlined in this Q&A http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php - If you feel the question has been solved, you should mark it as solved by accepting an answer. – Funk Forty Niner Aug 15 '16 at 11:23
  • thx Fred. I still working with add ,remove to cart. I may have problems and post it again soon. Hope to see ur reply :) – gosulove Aug 15 '16 at 12:08
  • @gosulove Glad to have been of help. You will have to post a new question and I hope you won't modify your existing question ;-) – Funk Forty Niner Aug 15 '16 at 12:09
  • yes. i am abt to post within 30 min. I meet a new problem – gosulove Aug 15 '16 at 12:36
  • @gosulove 30 mins. isn't long. Go make yourself a nice cup of coffee ;-) Post the new question and I'll see what I or others can do. Remember to put as much relevant information as possible. – Funk Forty Niner Aug 15 '16 at 12:38