简体   繁体   English

如何添加已检查的复选框的值?

[英]How to add values of checkboxes that have been checked?

I asked this question a few days ago but I'm a beginner and no one wanted to understand what I was talking about so I'll ask again as clear as possible. 我几天前问过这个问题,但我是初学者,没有人想知道我在说什么,所以我会尽快再问。 I have the following check boxes, they have values. 我有以下复选框,它们有值。 The values are their cost of the item that is on the menu. 值是菜单上项目的成本。 I understand that you can get a value by doing the $_POST function, but I'm not trying to echo values, I'm not trying to do the popular beginner PHP form technique of echoing the name you put in an input field. 我知道你可以通过执行$ _POST函数获得一个值,但我不是试图回显值,我不是试图用流行的初学者PHP形式技术来回显你输入字段中的名字。 What I am trying to achieve, is to add values of the check boxes that HAVE BEEN CHECKED. 我想要实现的是添加已经检查过的复选框的值。 So if a check box with the value of 10 is checked, and so is another with a value of 11, and another with a value of 5, I would like to echo that the price which would be 26. 因此,如果选中值为10的复选框,并且另一个值为11,另一个值为5,我想回显一下26的价格。

I completely understand that using isset to see if a checkbox is checked and then to echo whatever you want. 我完全理解使用isset来查看是否选中了复选框,然后回显你想要的任何内容。 I am new to PHP, and one of the popular answers I got was to use the value attribute like I've done so in my code and it was quoted as " will get you 123 in $_POST['foo'] after the form was submitted, if the user checked this checkbox.". 我是PHP的新手,我得到的一个流行的答案是使用value属性,就像我在我的代码中所做的那样,它被引用为“在表单之后会在$ _POST ['foo']中获得123已提交,如果用户选中此复选框。“ This isn't helpful, doesn't mention isset, not that that's important right now, but also only tells me it will get the 123 value if it has been checked. 这没有帮助,没有提到isset,不是现在这很重要,而且只告诉我如果已经检查它将获得123值。 That doesn't achieve what I want. 那不符合我的要求。

I understand I could assign variable values to each checkbox, and that would be a very sweet idea, but how would I then add ONLY check boxes that have been checked??? 我知道我可以为每个复选框分配变量值,这将是一个非常好的主意,但我怎么会添加已经检查过的复选框??? As in add the values of checked check boxes only so that the numbers are added up, because the numbers are their values. 与仅添加复选复选框的值一样,以便将数字相加,因为数字是它们的值。

Please no smart answers, I've had people give me solutions in JS, & even in jQuery, and people just telling me how to just echo a value. 请不要聪明的答案,我有人给我JS的解决方案,甚至在jQuery中,人们只是告诉我如何回应一个值。

Again, to make it clear, I am a beginner in PHP, and I have checked over 100 different questions now and I can't find what I'm looking for. 再说一次,为了说清楚,我是PHP的初学者,我现在已经检查了100多个不同的问题,我找不到我想要的东西。 Please do not give me answers that are not in PHP, I do not want to know how to echo the value of an element, although maybe that is where I'm going wrong, maybe I don't know how to get a value, but just getting a value doesn't do anything, and it doesn't add the number values of check boxes that have been checked together to get a total price of all the food you order. 请不要给我不是PHP的答案,我不想知道如何回应一个元素的值,虽然也许这就是我出错的地方,也许我不知道如何得到一个值,但只是获取一个值不会做任何事情,并且它不会添加已经一起检查的复选框的数值,以获得您订购的所有食物的总价。

Here is my HTML code: 这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
<title>Order</title>
<link rel="stylesheet" type="text/css" href="menu.css">
</head>
<body>
<form method="post" action="menu.php">
<ul>
    <li>
<ul class="menus">
<li><input type="checkbox" name="edible1" value="12">Sausage Rolls</li>
<li><input type="checkbox" name="edible2" value="8">Fries</li>
<li><input type="checkbox" name="edible3" value="10">Wings</li>
<li><input type="checkbox" name="edible4" value="11">Spinach Pinwheels</li>
<li><input type="checkbox" name="edible5" value="6">Garlci Bread</li>
</ul>
    </li>
    <li>
<ul class="menus">
    <li><input type="checkbox" name="edible6" value="18">Instant Pot Chicken</li>
    <li><input type="checkbox" name="edible7" value="20">Salmon Pattles</li>
    <li><input type="checkbox" name="edible8" value="23">Callco Cabbage</li>
    <li><input type="checkbox" name="edible9" values="22">Bacon Wrappend Jalapeno</li>
    <li><input type="checkbox" name="edible10" value="19">Potato Mash</li>
</ul>
    </li>
    <li>
<ul class="menus">
    <li><input type="checkbox" name="edible11" value="5">Edible Cookie Dough</li>
    <li><input type="checkbox" name="edible12" value="4">Ice Cream Sandwich</li>
    <li><input type="checkbox" name="edible13" value="7">Cheesecake</li>
    <li><input type="checkbox" name="edible14" value="3">Chocolate Cake</li>
    <li><input type="checkbox" name="edible15" value="2">Brownies</li>
</ul>
    </li>
    <li>
<ul class="menus">
    <li><input type="checkbox" name="edible16" value="5">Wine</li>
    <li><input type="checkbox" name="edible17" value="5">Beer</li>
    <li><input type="checkbox" name="edible18" value="5">Water</li>
    <li><input type="checkbox" name="edible19" value="5">Juice</li>
    <li><input type="checkbox" name="edible20" value="5">Coffee</li>
</ul>
    </li>
</ul>
<input type="submit" name="order" value="Order">
</form>
</body>
</html>

Here is my CSS if for some reason you care about that, maybe you want to know what the form looks like in my eyes idk: 这是我的CSS,如果由于某种原因你关心它,也许你想知道我眼中的形式是什么样的idk:

ul,li {
    list-style:none;
    padding : 10px;
}
  form > ul {
     display:flex;
}

I do not know what to do with PHP. 我不知道如何处理PHP。 I've made forms before adding together two input values, but what was the written values and not some given values, and it didn't have a complicated ultimatum. 我在添加两个输入值之前已经制作了表单,但是什么是书面值而不是某些给定值,并且它没有复杂的最后通.. I am aware of using isset to echo something if something has been or not been checked, I can get a value from an input field, I can add two values together (but not numbers). 我知道使用isset来回显一些东西,如果已经检查过某些东西,我可以从输入字段中获取一个值,我可以将两个值一起添加(但不是数字)。 I have been stuck on this for two days and so many hours have gone by I feel like at this point I must be doing something wrong that I'm oblivious to, I truly and genuinely don't know what to do. 我已经被困在这两天了,这么多个小时已经过去了,我觉得在这一点上我一定做错了,我不知道,我真的,真的不知道该怎么做。

Since your checkboxes are named with a prefix of edible , you can loop the $_POST array and find elements that start with that. 由于您的复选框以edible前缀命名,因此您可以循环$_POST数组并查找以该开头的元素。 If you find a match, add it to a $sum variable. 如果找到匹配项,请将其添加到$sum变量中。 Only checked checkboxes will be sent, so you do not have to account for unchecked checkboxes. 只会选中已选中的复选框,因此您无需考虑未选中的复选框。

if (isset($_POST['order'])) {
    $sum = 0;
    foreach ($_POST as $key => $value) {
        if (strpos($key, "edible") === true) {
            $sum += $value;
        }
    }
    echo $sum;
} else {
    echo "POST not sent.";
}

Then print $sum as you need it. 然后根据需要打印$sum

Alternatively, you can rename all your checkboxes, where they are all named name="edible[]" , making it a "HTML array". 或者,您可以重新命名所有的复选框,在那里他们被命名为name="edible[]" ,使其成为“HTML阵列”。

<ul class="menus">
    <li><input type="checkbox" name="edible[]" value="12">Sausage Rolls</li>
    <li><input type="checkbox" name="edible[]" value="8">Fries</li>
    <li><input type="checkbox" name="edible[]" value="10">Wings</li>
    <li><input type="checkbox" name="edible[]" value="11">Spinach Pinwheels</li>
    <li><input type="checkbox" name="edible[]" value="6">Garlci Bread</li>
</ul>

And do this for all checkboxes. 并为所有复选框执行此操作。

Then you simply do 然后你就做了

if (isset($_POST['order'])) {
     $sum = array_sum($_POST['edible']);
     echo $sum;
} else {
    echo "POST not sent.";
}

Change the name to name="edible[1]" , and so on. 将名称更改为name="edible[1]" ,依此类推。 When submitted, the values will be in an array in $_POST . 提交时,值将在$_POST中的数组中。 This can then be summed up using array_sum() . 然后可以使用array_sum()进行求和。

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

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