Solved

Select all or any checkboxes and Get the selected checkboxes

Posted on 2003-12-04
4
1,434 Views
Last Modified: 2008-03-17
Hello,

I have created a HTML form which has 4 checkboxes and one checkboxes, if checked, checks all the checkboxes, i mean it's a select all checkbox and i have written a JavaScript code for selecting all checkboxes.

But i am not able to get the checkboxes those are checked though a PHP script as the name of all the checkboxes had to keep same as invoke a JavaScipt function to select all checkboxes it user needs.

I do i get which checkboxes are checked. My checkbox html code is:
<input type="checkbox" name="list" value="Check All" onClick="this.value=check(this.form.list);">Select All
<input type="checkbox" name="list" value="1">Elastic Modulus
<input type="checkbox" name="list" value="2">Poisson's Ratio
<input type="checkbox" name="list" value="3">Coefficient of Thermal Expansion
<input type="checkbox" name="list" value="4">Density
0
Comment
Question by:maratmu
  • 2
4 Comments
 
LVL 6

Expert Comment

by:aolXFT
ID: 9875896
You need to send them as an array.

<input type="checkbox" name="list" value="1">Elastic Modulus

Would change to

<input type="checkbox" name="list[]" value="1">Elastic Modulus

Then for elastic Modulus you would check for the value 1 in the array $_POST['list'](if using post method), using in_array.

if(in_array(1, $_POST['list'])){
  $elastic_modulus = true;
}

I'm not sure how you'd tackle the Javascript though.
0
 
LVL 7

Expert Comment

by:petoskey-001
ID: 9877022
Another possible problem is using the same name for multiple checkboxes.  While this is valid HTML, php 4.3.3 for windows shows only the value of the last checked checkbox when using the POST method in the form.  If you use the GET method on your form you will still only get on result in your _Request["list"] variable, but you can pull the other responses from _SERVER["QUERY_STRING"].

The best solution would be to change the name of your checkboxes so each one is unique.  Then you don't have to deal with it.

Here is a link for implementing something similar that might work for you.

http://www.skyzyx.com/scripts/togglecheck.php

0
 
LVL 6

Accepted Solution

by:
aolXFT earned 60 total points
ID: 9877166
> The best solution would be to change the name of your checkboxes so each one is unique.  Then you don't have to deal with it.

That won't work for him, because he has a JS Select All, for which he needs to have all the names the same.

Having that said, if he changed the names to have unique names, and had some other attribute the same, such as ID that could implement the Select All function that would be a nice solution too.

Pulling responses from $_SERVER['QUERY_STRING'] is messy, and a very ugly hack in PHP. It also assumes use of the GET method.
0
 

Author Comment

by:maratmu
ID: 9878522
The Different ID funda and keeping name same worked , thank you
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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

17 Experts available now in Live!

Get 1:1 Help Now