Solved

Get id of input checkbox field if has been checked

Posted on 2014-02-20
8
1,256 Views
Last Modified: 2014-02-21
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
Comment
Question by:debbieau1
8 Comments
 
LVL 11

Expert Comment

by:Murfur
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 125 total points
Comment Utility
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
 
LVL 11

Assisted Solution

by:Murfur
Murfur earned 125 total points
Comment Utility
@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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 125 total points
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 125 total points
Comment Utility
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
 
LVL 1

Author Closing Comment

by:debbieau1
Comment Utility
thanks for all input
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
@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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now