Solved

Select all or any checkboxes and Get the selected checkboxes

Posted on 2003-12-04
4
1,435 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now