简体   繁体   English

php增加和减少产品数量?

[英]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?我是php的菜鸟,但我的问题是:我有一个while循环,它向我展示了db中的所有产品我试图在“立即购买”按钮旁边添加一个+和-数量按钮,按下它们会增加和减少(只显示数字不会影响数据库)但问题是数量不会超过数字 2。有人可以借给我一些大脑吗?

$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我已经尝试过用 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.我猜你正在学习 PHP,因为这不是用 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)我认为您应该为此使用 javascript(正如您所说的那样不会影响数据库)

I'm writing some code in jQuery to show an example of the plus button我正在用 jQuery 编写一些代码来显示加号按钮的示例

$('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.当然最好不要在代码中硬编码值并使用常量或变量(对于 1 限制),但我希望这个例子对你有所帮助。

I tried your javascript snippet and it worked perfectly.我试过你的 javascript 代码片段,它工作得很好。

That being said, is there a reason you are not using the HTML number input?话虽如此,您是否有理由不使用 HTML 数字输入?

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

Edit: I used the code from Daan's answer as the template but...编辑:我使用 Daan 的答案中的代码作为模板,但是...

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何增加/减少购物车中产品的数量? - How to increase / decrease the quantity of a product in a shopping cart? 如何使用PHP MySQL或javascript增加/删除产品数量。 或如何插入产品的数量值和复选框 - how to increase/delete quantity of a product in PHP MySQL or javascript. Or how to insert a quantity value of a product along with a checkbox 如何在数量框中添加增加和减少按钮 - How to add a increase and decrease button to quantity box reactjs如何增减数量? - How can I increase and decrease the quantity in reactjs? 如何分别增加和减少数量? - How can I increase and decrease the quantity seperately? JavaScript 或 jQuery 增加和减少数量时如何计算价格? - How to calculate price when increase and decrease quantity in JavaScript or jQuery? React-Redux 购物车 - 减少/增加商品数量 - React-Redux Shopping Cart - Decrease/Increase Item Quantity 如何增加或减少特定商品的数量? - How can I increase or decrease the quantity of a specific item? 为什么在 React 中增加或减少数量后我的总计没有更新? - Why is my Grand Total not updating after I increase or decrease the quantity in React? 我想使用JavaScript在点击图片(上下箭头.PNG)时增加和减少数量标签文本。 - I want to increase and decrease quantity label text on click on image (up down arrow .PNG) using JavaScript.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM