doudui1850 2011-04-14 09:32
浏览 10

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

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. 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:

<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

$(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.

  • 写回答

1条回答 默认 最新

  • dtv55860 2011-04-14 10:14
    关注
    $res=mysql_query($qu)
    

    change that to

    $res=mysql_query($q)
    

    and probably this too

    name="extras[]"" to name="extras[]"

    and i think array starts with 0, and the query would be:

    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++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计