Solved

Get id of input checkbox field if has been checked

Posted on 2014-02-20
8
1,378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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!

 
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 110

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 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