javascript - 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'); (var = 0; < 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 boxes" onclick="check_uncheck(true)" /> <input type="button" name="uncheckall" value="uncheck boxes" onclick="check_uncheck(false)" /> <input type="submit" value="save"> </form> snippet shows how works without connection database
i trying save data sent checklist database i'm stuck. thinking of using foreach don't know put in it.
i though of putting as:
foreach($_post['id'] $add){ insert database... }
how do this?
if fred-ii , xjstratedgebx suggested change name="chkboxarray" name="chkboxarray[]"
javascript code stop working.
<?php include '../conec.php'; mysql_select_db("test",$conec)or die('database not exist.') or die(mysql_error()); $sql = mysql_query("select * user 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 boxes" onclick="check_uncheck(true)" /> <input type="button" name="uncheckall" value="uncheck 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>
if change name of checkbox "chkboxarray" "chkboxarray[]", boxes checked when form submitted pass values array server under key of "chkboxarray".
basically, change line:
echo '<input type="checkbox" name="chkboxarray" value="'.$id.'" />'.$concate.'<br />';
to:
echo '<input type="checkbox" name="chkboxarray[]" value="'.$id.'" />'.$concate.'<br />';
as result, if var_dump $_post
super global, should see like:
array(1) { [chkboxarray]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "4" } }
in example above, checkboxes id's 3 , 4 checked, sent server.
once have array, inserting database depends heavily on you're trying accomplish , database's schema.
hope helps.
also, in fairness, @fred meant in comment.
edit 1
to make javascript work change of input name, you'll need update places in javascript referencing name of input new name (chkboxarray[]).
the resulting code should this:
<script type="application/javascript"> function check_uncheck(truefalse) { var boxes = document.forms[0]["chkboxarray[]"].length; var form = document.getelementbyid('checkform'); (var = 0; < boxes; i++) { if (truefalse) { form["chkboxarray[]"][i].checked = true; } else { form["chkboxarray[]"][i].checked = false; } } } </script>
i've created fiddle show works checking/unchecking boxes: https://jsfiddle.net/solvomedia/3ln468u3/
Comments
Post a Comment