简体   繁体   English

添加复选框,将价格添加到数据库中

[英]Adding up check boxes, adding the price into a database

I'm trying to add tick boxes and when the boxes are ticked a value adds up (a variable) that variable is then added into the database along with each checkbox once the form is submitted, I've been trying to modifiy this code but cant figure out how to not use the parseInt() function and output a single variable that I can then add into the database/Email reply to the customer. 我正在尝试添加复选框,当复选框被打勾时,将值相加(变量),然后在提交表单后将该变量与每个复选框一起添加到数据库中,我一直在尝试修改此代码,但是无法弄清楚如何不使用parseInt()函数并输出一个变量,然后我可以将其添加到数据库/通过电子邮件发送给客户。 I'm really stuck and would appreciate some help. 我真的很困,不胜感激。

(the options are suppose to actually be (deodoriser,carpet,carpetrepair,furniture,tabs,urine but im using demo options for now below in the insert staement they are the correct names) This is my HTML: (这些选项实际上应该是(除臭剂,地毯,地毯维修,家具,标签,尿壶,但是我现在在下面的插入位置中使用演示选项,它们是正确的名称)这是我的HTML:

<p><input type="checkbox" name="extras[]" value="option1" rel="11">furniture</p>
<p><input type="checkbox" name="extras[]"" value="option2" rel="12">tabs</p>
<p><input type="checkbox" name="extras[]" value="option3" rel="13">urine</p>
<p><input type="checkbox" name="extras[]" value="option4" rel="30">couch</p>
<p><input type="checkbox" name="extras[]" value="option5" rel="20">steam</p>
<span id="output"></span>

This is my javascript function 这是我的javascript函数

$(document).ready(function() {
    function recalculate() {
        var sum = 0;

        $("input[type=checkbox]:checked").each(function() {
            sum += parseInt($(this).attr("rel"));
        });

        $("#output").html(sum);
    }

    $("input[type=checkbox]").change(function() {
        recalculate();
    });
});

This is my email reply/datbase inserting at the moment 这是我目前插入的电子邮件回复/数据库

$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{

    $q="INSERT INTO bs_reservations (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff,deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff."','{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')";

    $res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
    $i++;
}

Thanks heaps for any advice/help in coding this. 感谢堆在编码方面的任何建议/帮助。 I know its a big question but I feel it will help a lot of people in the future. 我知道这是一个大问题,但我认为它将在将来帮助很多人。

$res=mysql_query($qu)

change that to 改成

$res=mysql_query($q)

and probably this too 可能也是

name="extras[]"" to name="extras[]" name="extras[]""改为name="extras[]"

and i think array starts with 0, and the query would be: 而且我认为数组以0开头,查询将是:

while ($i < $arr_num){

    $q  = "INSERT INTO bs_reservations"; 
    $q .= " (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff,deodoriser,carpet,carpetrepair,furniture,tabs,urine)";
    $q .= " VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff;
    $q .= "','".$idextra[0]."','".$idextra[1]."','".$idextra[2]."','".$idextra[3]."','".$idextra[4]."','".$idextra[5]."')";

    $res=mysql_query($q) or die('ERROR INSERTING: '.mysql_error());
    $i++;
}

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

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