PHP添加复选框 [英] php adding up checkboxes
问题描述
我有一个复选框列表显示正确,并重新检查所有已检查的值。我在我的代码的一部分中使用它,但也想获得数字或方格检查。复选框如下所示:
I have a list of check boxs that are displaying right and retunring the values of all that are checked. I use that in one part of my code but would also like to get the number or boxes check. The checkboxes are like so:
<div id="ironSet">
Include in Set
<input type="checkbox" name="iron[]" value="3" />
<span class="smallfont">3</span>
<input type="checkbox" name="iron[]" value="4" />
<span class="smallfont">4</span>
<input type="checkbox" name="iron[]" value="5" />
<span class="smallfont">5</span>
<input type="checkbox" name="iron[]" value="6" />
<span class="smallfont">6</span>
<input type="checkbox" name="iron[]" value="7" />
<span class="smallfont">7</span>
<input type="checkbox" name="iron[]" value="8" />
<span class="smallfont">8</span>
<input type="checkbox" name="iron[]" value="9" />
<span class="smallfont">9</span>
<input type="checkbox" name="iron[]" value="PW" />
<span class="smallfont">PW</span>
<input type="checkbox" name="iron[]" value="TW" />
<span class="smallfont">TW</span>
<input type="checkbox" name="iron[]" value="SW" />
<span class="smallfont">SW</span>
</div>
我在php评论表单中回显这些值,就像这样
I echo these values in a php review form like so
$iron = join(", ", $_REQUEST["iron"]);
echo (!empty($_REQUEST['iron'])) ? "<div class='reviewItem'><span class='reviewTitle'>In Set:</span>{$iron}</div>" : "";
这将返回所有选中的值(如果有的话)。有没有一种方法可以检查iron []
框的数量并将其存储在另一个变量中?我试图乘以一个价格变量检查的箱子数量。
This would return the values, if any, for all that are checked. Is there a way I can get the number of "iron[]"
boxes check as will and store it in another variable? I am trying to multiply the number of boxes checked by a price variable.
推荐答案
由于复选框是以数组的形式返回的,所以我应该能够简单地对它们进行计数。这是因为只有检查过的盒子才会发送到请求中。
As checkboxes are returned as an array, you should be able to simply do a count on them as far as I recall. This is because only boxes that are checked are sent though in the request.
$boxesChecked=count($_REQUEST["iron"]);
echo $boxesChecked;
从这里开始,您可以根据需要简单地应用您的逻辑:
from there, you can simply apply your logic as needed:
if($boxesChecked>5) // apply 20% discount
{
$dollarValue*=0.8;
}
这篇关于PHP添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!