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
Solved

Select all or any checkboxes and Get the selected checkboxes

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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…

856 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