Solved

Get id of input checkbox field if has been checked

Posted on 2014-02-20
8
1,306 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
ID: 39875525
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
ID: 39875550
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
ID: 39875553
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 11

Assisted Solution

by:Murfur
Murfur earned 125 total points
ID: 39876272
@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
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 125 total points
ID: 39876453
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 109

Accepted Solution

by:
Ray Paseur earned 125 total points
ID: 39876556
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
ID: 39878542
thanks for all input
0
 
LVL 58

Expert Comment

by:Gary
ID: 39878559
@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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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