簡體   English   中英

如何將復選框數組中的數據保存到數據庫中

[英]How do I save data from a checkbox array into database

 function check_uncheck(truefalse) { var boxes = document.forms[0].chkboxarray.length; var form = document.getElementById('checkForm'); for (var i = 0; i < boxes; i++) { if (truefalse) { form.chkboxarray[i].checked = true; } else { form.chkboxarray[i].checked = false; } } } 
 <form name="checkForm" id="checkForm" method="post" action="checkboxes1.php"> <input type="checkbox" name="chkboxarray" value="1" /><br /> <input type="checkbox" name="chkboxarray" value="2" /><br /> <input type="button" name="CheckAll" value="Check All Boxes" onclick="check_uncheck(true)" /> <input type="button" name="UncheckAll" value="Uncheck All Boxes" onclick="check_uncheck(false)" /> <input type="submit" value="Save"> </form> The snippet shows how it works without connection to a database 

我試圖保存從清單發送到數據庫的數據,但是我被卡住了。 我當時在考慮使用foreach,但是我不知道該放什么。

我雖然把它作為:

foreach($_POST['id'] as $add){
insert into database...
}

我該怎么做呢?

如果按照Fred-ii和xjstratedgebx的建議進行操作,那么我只是將name="chkboxarray" to name="chkboxarray[]"更改name="chkboxarray" to name="chkboxarray[]"那么javascript代碼將停止工作。

<?php
include '../conec.php';
mysql_select_db("test",$conec)or die('Database does not exist.') or die(mysql_error());

$sql = mysql_query("SELECT * FROM user WHERE state='Not Signed Up'");
?>

<form name="checkForm" id="checkForm" method="post" action="checkboxes1.php">
    <?php
    while($row = mysql_fetch_array($sql)){
        $id = $row['id'];
        $name = $row['name'];
        $lName= $row['lName'];
        $concate = $name.' '.$lName;
        echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';
    }


    ?>
    <!--<input type="checkbox" name="chkboxarray" value="1" /><br />
    <input type="checkbox" name="chkboxarray" value="2" /><br />-->
    <input type="button" name="CheckAll" value="Check All Boxes" onclick="check_uncheck(true)" />
    <input type="button" name="UncheckAll" value="Uncheck All Boxes" onclick="check_uncheck(false)" />
    <input type="submit" value="Save">

</form>

<script type="application/javascript">
function check_uncheck(truefalse){
    var boxes = document.forms[0].chkboxarray.length;
    var form = document.getElementById('checkForm');
    for(var i=0;i < boxes;i++){
        if (truefalse) {
            form.chkboxarray[i].checked=true;
        } else {
            form.chkboxarray[i].checked=false;
        }
    }
}
</script>

如果將復選框的名稱從“ chkboxarray”更改為“ chkboxarray []”,則在提交表單時選中的所有框都將其值作為數組傳遞給“ chkboxarray”鍵下的服務器。

基本上,更改此行:

echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';

至:

echo '<input type="checkbox" name="chkboxarray[]" value="'.$id.'" />'.$concate.'<br />';

結果,如果您使用var_dump $_POST超全局變量,則應該看到類似以下內容:

array(1) {
    [chkboxarray]=>
    array(2) {
        [0]=>
        string(1) "3"
        [1]=>
        string(1) "4"
    }
}

在上面的示例中,選中了ID為3和4的復選框,因此將其發送到服務器。

有了該數組后,將其插入數據庫將在很大程度上取決於您要完成的工作以及數據庫的架構。

希望能有所幫助。

同樣,公平地說,這正是@Fred在他的評論中的意思。

編輯1

為了使javascript能夠使用輸入名稱的更改,您需要將javascript中將輸入名稱引用到的所有位置更新為新名稱(chkboxarray [])。

產生的代碼應如下所示:

<script type="application/javascript">
    function check_uncheck(truefalse) {
        var boxes = document.forms[0]["chkboxarray[]"].length;
        var form = document.getElementById('checkForm');
        for (var i = 0; i < boxes; i++) {
            if (truefalse) {
                form["chkboxarray[]"][i].checked = true;
            } else {
                form["chkboxarray[]"][i].checked = false;
            }
        }
    }
</script>

我創建了一個小提琴以顯示此作品可用於選中/取消選中所有框: https : //jsfiddle.net/solvomedia/3Ln468u3/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM