Get id of input checkbox field if has been checked

I have a range of checkboxes and need to only save the ones, that have been checked.  They all have the same class (see below 'test').  

So in example below, I need to determine if input field with a class 'test' is checked, then get the id and value so I  can save to db.

It must be done in php

<input name="first" type="checkbox" id="first" value="1" class="test"/>
        <label for="first">first one</label>
<input name="second" type="checkbox" id="first" value="1" class="test"/>
        <label for="second">second one</label>
<input name="third" type="checkbox" id="first" value="1" class="test"/>
        <label for="third">third one</label>

Open in new window

<input name="first" type="checkbox" id="first" value="1" class="test"/>
        <label for="first">first one</label>
<input name="second" type="checkbox" id="second" value="1" class="test"/>
        <label for="first">second one</label>
<input name="third" type="checkbox" id="third" value="1" class="test"/>
        <label for="first">third one</label>

Open in new window

LVL 1
debbieau1Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
See if this article helps shed a little light on it.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

As is true of all input controls in HTML forms, there are two data elements passed to the action= script.  These are the name and the value.  All that other stuff like id, class, placeholder are useful on the client side of things, where they can be used with CSS and JavaScript to provide a better user experience.  But they mean nothing once the form has been submitted to the PHP action script.

To see what has been submitted, try adding these lines of code to your PHP action script.

var_dump($_POST); 
var_dump($_GET);

Open in new window

0
 
MurfurFull Stack DeveloperCommented:
Can you give a little more background to the requirement? I am a little lost in understanding the question because when the form is submitted the status and/or value of each checkbox will be saved regardless of whether it is checked or not i.e. those not checked will simply have no value. Also, if you are offering the user a choice you should probably use a radio group so that they can only select one. Or is it that you mean to save the selected ID to the db?

In PHP you are looking at something like:

$first = $_REQUEST['first'];
$second = $_REQUEST['second'];
$third = $_REQUEST['third'];
$result = '';

if ($first != ''){
    $result = 'first';
}else if ($second != ''){
    $result = 'second';
}else if ($first != ''){
    $result = 'third';
}

Open in new window



substituting $_REQUEST with $_GET or $_POST depending on your form method and you can then .

You can also use Javascript to retrieve the details of a ticked checkbox prior to submission and then manipulate it as you see fit:

var first = document.getElementById("first").checked;
var second = document.getElementById("second").checked;
var third = document.getElementById("third").checked;

if (first){
    alert('first');
}
elseif (second) {
    alert('second');
}
elseif (third) {
    alert('third');
}

Open in new window

0
 
GaryCommented:
if(isset($_POST['first'])){
// Checked
}

etc

If the checkbox is not checked then it would not be passed in the form submission so the isset would be false
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GaryConnect With a Mentor Commented:
when the form is submitted the status and/or value of each checkbox will be saved regardless of whether it is checked or not i.e. those not checked will simply have no value
Checkboxes that are not checked are not passed through the form submission. So there is no value to check, and trying to get the value will give an error.
0
 
MurfurConnect With a Mentor Full Stack DeveloperCommented:
@Cathal - I stand corrected.
Fortunately, the rest of my comment is valid and in my defence, it was well past midnight and I was firing off a couple of quick answers before retiring!
0
 
Chris StanyonConnect With a Mentor Commented:
As Cathal has already said, a checkbox only gets submitted if it's ticked.

That said, the class has nothing to do with what's posted to your script. And the ID is never passed - only the name

Both block of your HTML code are incorrect.

In the first block, all the IDs are the same which is not allowed. In the second block, all the labels are for an element with an ID of 'first'!

Use the labels from your first block and the checkboxes from the second block.
0
 
debbieau1Author Commented:
thanks for all input
0
 
GaryCommented:
@Murfur
If you had said you had been drinking then it would have been a better defence in the WebDev Zone ;o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.