Question

Not the usual case of just knowing how to "insert" checkboxes into the database , I have a form with several checkboxes ...about 25 checkboxes each with the same name and values from 1-25 , Now the thing is I need them to have the same name in order to run a script which prevents the selection of no more than 4 checkboxes ...

HTML Code

<input type="checkbox" name="ckb" id="EngineeringWorkshops" value=1 onclick='chkcontrol(0)';>
<label for="EngineeringWorkshop"> Engineering Workshops           </label>

PHP Code

$engineeringworkshops = mysqli_real_escape_string($dbcon, $_POST['cob']); 


    $sql="INSERT INTO courses (studentid, engworkshops)
    VALUES ('$studentid', '$engineeringwokshops')";

engworkshops column type is tinyint(4) default value set to 0

Was it helpful?

Solution

Use such html:

<form>
<input type='checkbox' name='data[]' value='YourId' /> Data
<input type='checkbox' name='data[]' value='AnotherId' /> AnotherData
<input type='checkbox' name='data[]' value='OneMoreId' /> OneMoreData
// others
</form>

How to handle this data in php?

Use this:

$engineeringworkshops = mysqli_real_escape_string($dbcon, $_POST['cob']); 
// handle data from checkboxes
foreach($_POST['data') as $item) {
    // do something in item
    $sql="INSERT INTO courses (studentid, engworkshops) VALUES ('$item', '$engineeringwokshops')";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top