简体   繁体   中英

php increase and decrease quantity of product?

im noob at php but my question is this: im having a while loop which presents to me all the products from the db im trying to add a + and - quantity buttons next to the buy now button that will increase and decrease by pressing them (just display the number not affect the db) but the problem is that the quantity number won't pass the number 2. Can anyone lend me some brains with this one?

$value = 1; //to be displayed
if(isset($_POST['incqty'])){
$value= $value +1;
echo $value;
}

if(isset($_POST['decqty'])){
$value = $value -1;                                            
echo $value;
}

<form method='post'>
<input type='hidden' name='item'/>
<td><button name='incqty'>+</button><input type='text' size='1' name='item' value='$value'/><button name='decqty'>-</button></td>
</form>

I already tried to do it with js

<form name="f1"> <input type='text' name='qty' id='qty' />
<input type='button' name='add' onclick='javascript:document.getElementById("qty").value++;' value='+'/> 
<input type='button' name='subtract' onclick='javascript: document.getElementById("qty").value--;' value='-'/> 
</form>

but the buttons were unclickable...

I'm guessing you're learning PHP and because of that not doing this with javascript. Below code should work for you:

<?php
$value = isset($_POST['item']) ? $_POST['item'] : 1; //to be displayed
if(isset($_POST['incqty'])){
   $value += 1;
}

if(isset($_POST['decqty'])){
   $value -= 1;                                            
}
?>

<form method='post' action='<?= $_SERVER['PHP_SELF']; ?>'>
   <!--<input type='hidden' name='item'/> Why do you need this?-->
   <td>
       <button name='incqty'>+</button>
       <input type='text' size='1' name='item' value='<?= $value; ?>'/>
       <button name='decqty'>-</button>
   </td>
</form>

If I understood the question, you need to show the values of the items in the cart and modify it by pressing + and - button.

I think you shoud use javascript for this (as you are saying that no affects database)

I'm writing some code in jQuery to show an example of the plus button

$('button[name="incqty"]').bind('click', funcion(){
  var actual = $('input[name="intem"]').val();
  if (actual <= 1) {
    actual = actual +1;
  } 
  $('input[name="intem"]').val(actual);
});

Of course is better not to hardcode values in the code and use constants or variables (for the 1 limit) but I hope this example help you.

I tried your javascript snippet and it worked perfectly.

That being said, is there a reason you are not using the HTML number input?

<input type="number" name="quantity" min="1" max="99">

Edit: I used the code from Daan's answer as the template but...

<?php
$value = isset($_POST['item']) ? $_POST['item'] : 1; //to be displayed
?>

<form method='post' action='<?= $_SERVER['PHP_SELF']; ?>'>
   <!--<input type='hidden' name='item'/> Why do you need this?-->
   <td>
       <input type="number" name="item" min="1" max="99" value="<?= $value; ?>">
   </td>
</form>
<input type="button" value = "-" onclick="minusbot()" />    
<input type="number" name="kgcount" id = "kilohere" value = "1" >
<input type="button" value = "+" onclick="plusbot()" />                 

<script>
    function minusbot()
    {
        document.getElementById('kilohere').value = parseInt(document.getElementById('kilohere').value) - 1;
        var checknumifzero = parseInt(document.getElementById('kilohere').value);

        if(checknumifzero < 1) //preventing to get negative value
        {
            document.getElementById('kilohere').value = 1;
        }
    }   

    function plusbot()
    {
        document.getElementById('kilohere').value = parseInt(document.getElementById('kilohere').value) + 1;
        var checknumiffifty = parseInt(document.getElementById('kilohere').value);

        if(checknumiffifty > 50) //just a limit
        {
            document.getElementById('kilohere').value = 50;
        }
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM