繁体   English   中英

如何将选中的复选框的值保存到数据库

[英]How to save the value of the checked checkbox to the database

我这里的复选框具有不同的价格,并且每个都有不同的标题。 当用户单击复选框时,它将在总计字段中查看价格。 现在,当用户单击“保存”按钮时,我现在要将总计保存到数据库,并将选中的复选框的标题保存到类别。 将保存到数据库的第一行的标题必须用逗号(,)分隔。

这是我的HTML代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
    <link rel="stylesheet" type="text/css" media="screen" href="css/master.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <!--<script type="text/javascript" src="js/jquery.min.js"></script>-->
    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
    <form action="" method="post">
        <input type="checkbox" name="checkbox1" value="₱20"> Red </br>
        <input type="checkbox" name="checkbox1" value="₱30"> Blue </br>
        <input type="checkbox" name="checkbox1" value="₱40"> Green </br>
        </br></br>
        Total: <input type="text" name="total" readonly>
        <input type="submit" name="save" value="SAVE">
    </form>
    <script type="text/javascript">
    $(function(){
        //bind the change event to the checkboxes
        $('input[name="checkbox1"]').change(function(){
            var total = 0;
            //get value from each selected ckeck box
            $('input[name="checkbox1"]:checked').each(function(){
                var tval = $(this).val();
                //remove ₱ sign from value
                //convert it to a flot
                //plus it to the total
                total += parseFloat(tval.replace("₱",""));                
            });
            //finally display the total with a ₱ sign
            $('input[name="total"]').val("₱ " + total);
        });
    });
    </script>
</body>
</html>

我不知道如何将复选框的标题保存到数据库的一行(CATEGORY)。 总价也必须保存在数据库的TOTAL字段中。

表名:PRODUCTS

列:CATEGORY,总数

保存在数据库中的示例数据:

类别:[红色,蓝色]

总计:[50]

给他们起个不同的名字

<input type="checkbox" name="Red" value="₱20"> Red </br>
<input type="checkbox" name="Blue" value="₱30"> Blue </br>
<input type="checkbox" name="Green" value="₱40"> Green </br>

并更改您的jQuery一点:

//bind the change event to the checkboxes
$('input[name="checkbox"]').change(function(){..}

然后访问属性名称:

var name = $(this).attr("name");

演示


$(function(){
    var total = 0;
    var colors = "";
    $("input[type=checkbox]").change(function(e) {

        var selected_color = $('input[type=checkbox]:checked');
        colors = selected_color.map(function() {
            return $(this).attr("name");
        }).get().join(', ');
        //alert(colors);


         selected_color.each(function(){
           var tval = $(this).val();
           total += parseFloat(tval.replace("₱",""));                
        });
        //alert(total);
    });

    $( "form" ).submit(function( event ) {
        $.ajax({
          type: "POST",
          url: "your_script.php",
          data: { category:colors, total: total},
          success:  function(data) {
                  alert('success'); 
          }
        });
    });
});

的PHP

echo $_POST['categor'];
echo $_POST['total'];

至于插入,这很简单,您没有指定使用哪个驱动程序,但是您可以向mysqli咨询@Ghost答案

演示

如果需要颜色,则需要从绑定的复选框中获取下一个兄弟姐妹,然后可以将颜色设置为价格,从而为颜色创建另一个隐藏的表单。 粗略的例子:

<form action="" method="post">
    <input type="checkbox" name="checkbox1" value="20"> Red </br>
    <input type="checkbox" name="checkbox1" value="30"> Blue </br>
    <input type="checkbox" name="checkbox1" value="40"> Green </br>
    </br></br>
    <!-- hidden input colors -->
    <input type="hidden" name="colors" value="" />
    Total: <input type="text" name="total" readonly>
    <input type="submit" name="save" value="SAVE">
</form>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){

    var colors = [];
    //bind the change event to the checkboxes
    $('input[name="checkbox1"]').change(function(){
        var total = 0;
        //get value from each selected ckeck box
        $('input[name="checkbox1"]:checked').each(function(){
            var tval = $(this).val();
            total += parseFloat(tval);
        });
        //finally display the total with a ₱ sign
        $('input[name="total"]').val("₱ " + total);

        // handle colors
        var color = $.trim($(this)[0].nextSibling.nodeValue); // get the name
        var i = colors.indexOf(color);
        if(i != -1) {
            colors.splice(i, 1); // remove if unchecked
        } else {
            colors.push(color); // push if checked
        }
        var temp = colors.join(', ');
        $('input[name="colors"]').val(temp);

    });

});
</script>

PHP:

<?php

$db = new mysqli('localhost', 'username', 'password', 'database');
$stmt = $db->prepare('INSERT INTO `PRODUCTS` (`CATEGORY`, `TOTAL`) VALUES (?, ?)');

if(isset($_POST['save'])) {
    $total = (int) str_replace('₱ ', '', $_POST['total']); // remove peso sign
    $colors = $_POST['colors'];
    $stmt->bind_param('si', $colors, $total);
    $stmt->execute();
}

?>

好吧,这似乎是超级非凡标签元素的问题!!! (我是如此愚蠢,我无法克服自己的动力-.-)

您可以将其放在这样的标签中:

红色

因此每个标签标识每个复选框

因此,您可以(如果是PHP):

$ labels = $ _POST ['labels'];

并假设您有一个像这样的规范化数据库(其他所有东西都只能在字符串上玩):

| ID | 类别|

for($ i = 0; $ i <count($ labels); $ i ++){mysqli_query($ con,“插入CATEGORY(类别名称)值('” + $ labels [$ i]“');”); }

PD:此代码未经测试。

您需要编写一些服务器端代码,这些代码将采用表格数据并将其写入数据库-通常以PHP或ASP编写,并用SQL将数据写入数据库。

暂无
暂无

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

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