Solved

Select all or any checkboxes and Get the selected checkboxes

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
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…
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.

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