Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1444
  • Last Modified:

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

0
debbieau1
Asked:
debbieau1
4 Solutions
 
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
 
GaryCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MurfurFull 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 StanyonCommented:
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
 
Ray PaseurCommented:
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
 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now