1

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

7
  • 1
    and the question is ? Commented Apr 25, 2014 at 22:22
  • 1
    Using the same name is definitely not going to work. You could use an array, but then you would have to change your back-end as well: name="ckb[SOME_ID]" or name="ckb[]" if you can identify them uniquely by the values. Commented Apr 25, 2014 at 22:24
  • @jeroen so if I use different names , all I gotta do is create a field for each checkbox and a php value similar to the one above ? should I make the checkbox values all the same ? Commented Apr 25, 2014 at 22:27
  • @pawel7318 well I was asking if its even possible and how if so ... Commented Apr 25, 2014 at 22:28
  • @Azizi I don't care much for checkbox values, I normally use a unique id like name="ckb[1]" name="ckb[25]" and use isset() on the server-side to see which ones were checked. Commented Apr 25, 2014 at 22:35

1 Answer 1

1

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')";
}
Sign up to request clarification or add additional context in comments.

9 Comments

You need to put the (unique...) value back in or use a unique ID for each checkbox to know which ones were checked.
Yes, id should be unique anyway (not only in this case). Code from my answer will insert only data, which was selected. $_POST or $_GET will contain only selected checboxes ID's, wouldn't it?
No, it will contain only the values, ID's are not sent to the server. If two are checked they will have indexes of 0 and 1, but it could be checkbox 1 and 2, 1 and 3 or 2 and 3. And that's with only 3 checkboxes...
Aaaaaaah my bad. I know I know I know this, sorry. I mixed up attributes value and id. My mistake. Have to go to bed, I think. Thank you. I edited my answer. Sorry.
shouldn't it be something like $engineeringworkshops = mysqli_real_escape_string($dbcon, $_POST['data']); ? and do I have to define $item ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.