472,958 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

PHP-Javascript method for auto-selecting checkbox groups

I am producing a form using PHP on the back end and Javascript on the
front end. The resulting script will come to the browser as follows:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. <!--
  3. function selectAll() {
  4. moveElement = eval("document.imageForm.move" + "[]");
  5. alert(moveElement);
  6. if (document.imageForm.select_all[0].checked) {
  7. for (i = 0; i < document.imageForm.move.length; i++) {
  8. document.imageForm.move[i].checked = true;
  9. }
  10. }
  11. }
  12.  
  13. function deSelectAll() {
  14. if (!document.imageForm.select_all[0].checked ||
  15. document.imageForm.deselect_all[0].checked) {
  16. for (i = 0; i < document.imageForm.move.length; i++) {
  17. document.imageForm.move[i].checked = false;
  18. }
  19. }
  20. }
  21. //-->
  22. </script>
  23.  
This will work with a checkbox group that has to have this naming
convention for PHP:

Expand|Select|Wrap|Line Numbers
  1. <input type=checkbox name=move[] value="myImage.gif"> - Move?
  2.  
I will also have a checkbox that will "select all" and "de-select
all":

Expand|Select|Wrap|Line Numbers
  1. <input type=checkbox name=select_all value=1 onChange="selectAll()"> -
  2. Select All&nbsp;&nbsp;
  3. <input type=checkbox name=deselect_all value=1
  4. onChange="deSelectAll()"> - De-select All
  5.  
I am not familiar at how Javascript can work with non-standard naming
conventions for event actions, can someone tell me what I'm doing
wrong? This fails at least in Mozilla 1.6

Thanx
Phil
Jul 17 '05 #1
9 4615
"Phil Powell" <so*****@erols.com> wrote:
This will work with a checkbox group that has to have this naming
convention for PHP:
I will also have a checkbox that will "select all" and "de-select
all":


Checkboxes don't have onChange() events, they have onclick() events.

If you want to ease your pain a bit, you could consider using my "Checkbox
Group" library, which would handle all this for you quite easily:
http://www.mattkruse.com/javascript/checkboxgroup/

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 17 '05 #2
On Tue, 13 Apr 2004 12:22:45 -0700, Phil Powell wrote:
I am not familiar at how Javascript can work with non-standard naming
conventions for event actions, can someone tell me what I'm doing
wrong? This fails at least in Mozilla 1.6


Give your checkboxex an id and use document.getElementById() to select
them in Javascript.

You can dynamically create your selectAll() function from PHP the same way
you create your checkboxes

function selectAll() {
document.getElementById('1').checked = true;
// ...
document.getElementById('10').checked = true;
}

Regards,
Henk Burgstra
Jul 17 '05 #3
> I am not familiar at how Javascript can work with non-standard naming
conventions for event actions, can someone tell me what I'm doing
wrong? This fails at least in Mozilla 1.6


Give your checkboxes an id and use document.getElementById() to select
them in Javascript.

You can dynamically create your selectAll() function from PHP the same way
you create your checkboxes

function selectAll() {
document.getElementById('1').checked = true;
// ...
document.getElementById('10').checked = true;
}

Regards,
Henk Burgstra
Jul 17 '05 #4
Phil Powell wrote:
I am producing a form using PHP on the back end and Javascript on the
front end. The resulting script will come to the browser as follows:

Expand|Select|Wrap|Line Numbers
  1.  <script>
  2.  <!--
  3.    function selectAll() {
  4.     moveElement = eval("document.imageForm.move" + "[]");
  5.     alert(moveElement);
  6.     if (document.imageForm.select_all[0].checked) {
  7.      for (i = 0; i < document.imageForm.move.length; i++) {
  8.       document.imageForm.move[i].checked = true;
  9.      }
  10.    }
  11.   }
  12.   function deSelectAll() {
  13.    if (!document.imageForm.select_all[0].checked ||
  14.  document.imageForm.deselect_all[0].checked) {
  15.     for (i = 0; i < document.imageForm.move.length; i++) {
  16.      document.imageForm.move[i].checked = false;
  17.     }
  18.    }
  19.   }
  20.  //-->
  21.  </script>
  22.  

This will work with a checkbox group that has to have this naming
convention for PHP:

Expand|Select|Wrap|Line Numbers
  1.  <input type=checkbox name=move[] value="myImage.gif"> - Move?
  2.  

I will also have a checkbox that will "select all" and "de-select
all":

Expand|Select|Wrap|Line Numbers
  1.  <input type=checkbox name=select_all value=1 onChange="selectAll()"> -
  2.  Select All&nbsp;&nbsp;
  3.  <input type=checkbox name=deselect_all value=1
  4.  onChange="deSelectAll()"> - De-select All
  5.  

I am not familiar at how Javascript can work with non-standard naming
conventions for event actions, can someone tell me what I'm doing
wrong? This fails at least in Mozilla 1.6

Thanx
Phil

Your problem is reasonably common (search google groups for select box
and php and I'm sure you'll find a few recent posts (one or two from me)
related to this.

However... there are two methods that you can use to refer to an object
in a form in javascript - I'm a newbie (six months) but think I'm
getting pretty strong... someone though might have some other
method/solution... however, use something like

boxLength=document.forms["formGroups"].elements["userSelected[]"].length;

where by 'formGroups' is the name of your form and 'userSelect[]' is the
name of your select box.

Using my above example, boxLength would equal the total number of
selections in a multi-select box.

Also... put quotes around your INPUT TAG name field... thus

Use <input type=checkbox name="move[]" value="myImage.gif"> not <input type=checkbox name=move[] value="myImage.gif">


as the [] might cause some grief on some web browsers...

Lastly - I just learned that a mis-use of eval is common - there are
other methods that you can use that use less resources and faster (for
example, from a numeric point of view you could use something like
parseInt() - Do a search on google groups for a recent message I got
which explains the reasons - search for "why is eval evil" in
comp.lang.javascript for a detailed explanation.

Hope something above gives you some help...

Regards
Randell D.
Jul 17 '05 #5
Following occurred when my colleague tested on his machine using IE
6.0 SP 4 for Win2000 (I am using Linux/Mozilla):

Ok, back to the drawing board, it didn't work when my colleague tested
on his machine using IE 6.0 SP4 for Win2K:

I tried with the HTML code below, but IE spat out the following:

Line: 42
Char: 1
Error: 'document.imageForm.move[]' is null or not an object
Code: 0
URL: file://C:\Documents and Settings\eduardo\Desktop\test.html

HTML code:

<html>
<head>
<title></title>
<body>
<script>
<!--
~ function selectAll(checkState, selectStatus, obj) {
~ if (document.imageForm.select_all.checked ||
document.imageForm.deselect_all.checked) {
~ for (i = 0; i < obj.length; i++) {
~ obj[i].checked = checkState;
~ }
~ if (selectStatus.toLowerCase() == 'select')
document.imageForm.deselect_all.checked = false;
~ if (selectStatus.toLowerCase() == 'deselect')
document.imageForm.select_all.checked = false;
~ }
}
//-->
</script>

<table>
<tr>
~ <td bgcolor=ffffff><a
href="./images/mu-spin/kyrka.jpg">kyrka.jpg</a></td>
~ <td bgcolor=ffffff>Show Metadata</td>
~ <td bgcolor=ffffff><input type=checkbox name="move[]"
value="kyrka.jpg"> - Move?</td>

</tr>
<tr>
~ <td bgcolor=ffffcc><a
href="./images/mu-spin/banner.jpg.jpeg">banner.jpg.jpeg</a></td>
~ <td bgcolor=ffffcc><a
href=index.php?section=image&action=edit&chooseAlb um=1&album=mu-spin&id=147>Show
Metadata</a></td>
~ <td bgcolor=ffffcc><input type=checkbox name="move[]"
value="banner.jpg.jpeg"> - Move?</td>
</tr>
<tr>

~ <td bgcolor=ffffff><a
href="./images/mu-spin/kyrka_gray.jpg">kyrka_gray.jpg</a></td>
~ <td bgcolor=ffffff><a
href=index.php?section=image&action=edit&chooseAlb um=1&album=mu-spin&id=145>Show
Metadata</a></td>
~ <td bgcolor=ffffff><input type=checkbox name="move[]"
value="kyrka_gray.jpg"> - Move?</td>
</tr>
<tr>
~ <td colspan=3 bgcolor=ddddee>
~ <input type=checkbox name="select_all" value=1
onClick="selectAll(true, 'select', document.imageForm['move[]'])"> -
Select All&nbsp;&nbsp;<input type=checkbox name="deselect_all" value=1
onClick="selectAll(false, 'deselect', document.imageForm['move[]'])">
-
De-Select All</td>

</tr>
</table>
</body>
</html>

Phil

"Matt Kruse" <ne********@mattkruse.com> wrote in message news:<c5*********@news3.newsguy.com>...
"Phil Powell" <so*****@erols.com> wrote:
This will work with a checkbox group that has to have this naming
convention for PHP:
I will also have a checkbox that will "select all" and "de-select
all":


Checkboxes don't have onChange() events, they have onclick() events.

If you want to ease your pain a bit, you could consider using my "Checkbox
Group" library, which would handle all this for you quite easily:
http://www.mattkruse.com/javascript/checkboxgroup/

Jul 17 '05 #6
Does this work in IE and Opera? I have no way of testing in either
browser as I am on a Linux box and have no rights to install anything
onto it.

Phil

Henk Burgstra <eg****@xs4all.nl> wrote in message news:<pa****************************@xs4all.nl>...
I am not familiar at how Javascript can work with non-standard naming
conventions for event actions, can someone tell me what I'm doing
wrong? This fails at least in Mozilla 1.6


Give your checkboxes an id and use document.getElementById() to select
them in Javascript.

You can dynamically create your selectAll() function from PHP the same way
you create your checkboxes

function selectAll() {
document.getElementById('1').checked = true;
// ...
document.getElementById('10').checked = true;
}

Regards,
Henk Burgstra

Jul 17 '05 #7
On Wed, 14 Apr 2004 06:33:35 -0700, Phil Powell wrote:

Give your checkboxes an id and use document.getElementById() to select
them in Javascript.

You can dynamically create your selectAll() function from PHP the same way
you create your checkboxes

function selectAll() {
document.getElementById('1').checked = true;
// ...
document.getElementById('10').checked = true;
}

Regards,
Henk Burgstra


Does this work in IE and Opera? I have no way of testing in either
browser as I am on a Linux box and have no rights to install anything
onto it.

Phil


Yes, it does. I know it works in IE and I tested it for you in Opera 7.50
/ Linux
Please don't top post.

Regards,
Henk Burgstra

Jul 17 '05 #8
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
Following occurred when my colleague tested on his machine using IE
6.0 SP 4 for Win2000 (I am using Linux/Mozilla):


Aren't you missing a <form> tag entirely?

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 17 '05 #9
JRS: In article <Gg1fc.110637$oR5.109407@pd7tw3no>, seen in
news:comp.lang.javascript, Reply Via Newsgroup <reply-to-
ne*******@please.com> posted at Wed, 14 Apr 2004 02:23:34 :
- I'm a newbie (six months) Lastly - I just learned that a mis-use of eval is common - there are
other methods that you can use that use less resources and faster (for
example, from a numeric point of view you could use something like
parseInt() - Do a search on google groups for a recent message I got
which explains the reasons - search for "why is eval evil" in
comp.lang.javascript for a detailed explanation.


For that, the OP should be referred to the c.l.j newsgroup FAQ. It's
generally better to cite a specific reference rather than to call for a
search.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lawrence | last post by:
I haven't been able to reach www.php.net for days. Most of the rest of the web is working for me, though I've bad trouble reaching any English sites. Anyone else having trouble?
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
59
by: Lennart Björk | last post by:
Hi All, I have a tiny program: <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>MyTitle</title> <meta...
1
by: jdurden | last post by:
am currently woking on building this site: www.maverick-spirit.com. everything seems to be fine except the press releases page. When you click on one of the press releases in the left column under...
0
by: tsivaraman | last post by:
I am trying to build php-5.2.1 in RedHat Linux 9. I have installed libxml2-2.6.11,mysql-5.0.33,httpd-2.2.4(apache) successfully.When i do 'make' from the php directory,i get the following...
0
by: Yannick | last post by:
Hi, I'm Julien from France, We have recently install a new Web Server for my company The server is composed : - Linux RedHat RHEL4 U4 - Httpd-2.0.52-27.ent - Oracle Database 10.2.0.1
26
by: webrod | last post by:
Hi, I have some php pages with a lot of HTML code. I am looking for a HTML validator tool (like TIDY). TIDY is not good enough with PHP tags (it removes a lot of php code). Do you have any...
0
by: Benjamin Grieshaber | last post by:
Hi, I´m on SuSE 9.3 with xmlrpc-c and xmlrpc-c-devel installed (ver. 0.9.10) I tried to compile php with xmlrpc support and got the following errors: ...
3
by: jaddi1 | last post by:
Hi, I am trying to make a multi-level drop-down menu similar to what is seen here: http://www.cssplay.co.uk/menus/simple_vertical.html. My problem is that some of the menu will be populated from...
4
by: mechphisto | last post by:
I'm working on a friend's box, Fedora Core 6. It has PHP 5.1.6. I need to install mcrypt into it, and the only way I can find to do it is from source then recompile PHP. So I did all that, and got...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.