473,809 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23 '05 #1
7 1880
"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 23 '05 #2
I'm sorry but please provide a very simple tutorial on this, you went so
far over my head that you were on Jupiter and I'm underground.

Phil

---------------------------------------
TCL and PHP: The Superior Web Languages!
javascript: Is Cool
ASP: why?
http://valsignalandet.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
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=docum ent.forms["formGroups "].elements["userSelect ed[]"].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.javas cript for a detailed explanation.

Hope something above gives you some help...

Regards
Randell D.
Jul 23 '05 #4
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.image Form.move[]' is null or not an object
Code: 0
URL: file://C:\Documents and Settings\eduard o\Desktop\test. html

HTML code:

<html>
<head>
<title></title>
<body>
<script>
<!--
~ function selectAll(check State, selectStatus, obj) {
~ if (document.image Form.select_all .checked ||
document.imageF orm.deselect_al l.checked) {
~ for (i = 0; i < obj.length; i++) {
~ obj[i].checked = checkState;
~ }
~ if (selectStatus.t oLowerCase() == 'select')
document.imageF orm.deselect_al l.checked = false;
~ if (selectStatus.t oLowerCase() == 'deselect')
document.imageF orm.select_all. checked = false;
~ }
}
//-->
</script>

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

</tr>
<tr>
~ <td bgcolor=ffffcc> <a
href="./images/mu-spin/banner.jpg.jpeg ">banner.jpg.jp eg</a></td>
~ <td bgcolor=ffffcc> <a
href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=147>Sho w
Metadata</a></td>
~ <td bgcolor=ffffcc> <input type=checkbox name="move[]"
value="banner.j pg.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&a ction=edit&choo seAlbum=1&album =mu-spin&id=145>Sho w
Metadata</a></td>
~ <td bgcolor=ffffff> <input type=checkbox name="move[]"
value="kyrka_gr ay.jpg"> - Move?</td>
</tr>
<tr>
~ <td colspan=3 bgcolor=ddddee>
~ <input type=checkbox name="select_al l" value=1
onClick="select All(true, 'select', document.imageF orm['move[]'])"> -
Select All&nbsp;&nbsp; <input type=checkbox name="deselect_ all" value=1
onClick="select All(false, 'deselect', document.imageF orm['move[]'])">
-
De-Select All</td>

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

Phil

"Matt Kruse" <ne********@mat tkruse.com> wrote in message news:<c5******* **@news3.newsgu y.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 23 '05 #5
"Phil Powell" <so*****@erols. com> wrote in message
news:1c******** *************** ***@posting.goo gle.com...
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 23 '05 #6
No I just forgot to include it within the code I pasted here. Here is
the corrected code that now works in IE, Opera, Konqueror and Mozilla,
according to tests I did and a colleague did:

<form name="imageForm " method=post action=index.ph p
onSubmit='setHa sSelectedAlbum( ); return willMoveImages( "Any metadata
associated with the images moved will be altered by moving the images.
Are you sure?")'>
<tr>

<td colspan=6>
<script type="text/javascript">
<!--
function willMoveImages( msg) {
confirmWindow = window.confirm( msg);
if (confirmWindow) return true;
return false;
}
//-->
</script>
<noscript>
<!-- Noscript option -->
</noscript>
<table border=0 width=100%>
<tr>
<td bgcolor=eeeedd colspan=3><a
href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin>Go
back to your previous listing</a></td>

</tr>
<tr>
<td bgcolor=ddddee> Image (click to view)</td> <td
bgcolor=ddddee> Click to view image metadata if link is found</td> <td
bgcolor=ddddee> &nbsp;</td>
</tr>
<script type="text/javascript">
<!--

var hasSelectedAlbu m = false;

function setHasSelectedA lbum() {
if (!hasSelectedAl bum) hasSelectedAlbu m = true;
}

function selectAll(check State, selectStatus, obj) {
if (document.image Form.select_all .checked ||
document.imageF orm.deselect_al l.checked) {
for (i = 0; i < obj.length; i++) {
obj[i].checked = checkState;
}

if (selectStatus.t oLowerCase() == 'select')
document.imageF orm.deselect_al l.checked = false;
if (selectStatus.t oLowerCase() == 'deselect')
document.imageF orm.select_all. checked = false;
}
}
//-->
</script>
<noscript>
<!-- THERE IS NO NON-JAVASCRIPT EQUIVALENT TO THIS CODE -->

</noscript>
<tr>
<td bgcolor=ffffff> <a
href="./images/mu-spin/kyrka.jpg">kyrk a.jpg</a></td>
<td bgcolor=ffffff> Show Metadata</td>
<td bgcolor=ffffff> <input type=checkbox id="selectable0 " name="move[]"
value="kyrka.jp g"> - Move?</td>
</tr>

<tr>
<td bgcolor=ffffcc> <a
href="./images/mu-spin/banner.jpeg">ba nner.jpeg</a></td>
<td bgcolor=ffffcc> <a
href=index.php? section=image&a ction=edit&choo seAlbum=1&album =mu-spin&id=
147>Show Metadata</a></td>
<td bgcolor=ffffcc> <input type=checkbox id="selectable1 " name="move[]"
value="banner.j peg"> - 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&a ction=edit&choo seAlbum=1&album =mu-spin&id=
145>Show Metadata</a></td>
<td bgcolor=ffffff> <input type=checkbox id="selectable2 " name="move[]"
value="kyrka_gr ay.jpg"> - Move?</td>
</tr>
<tr>
<td colspan=3 bgcolor=ddddee>
<input type=checkbox id="checkall" name="select_al l" value=1
onClick="select All(true, 'select', document.imageF orm['move[]'])"> -
Select All&nbsp;&nbsp;

<input type=checkbox id="uncheckall " name="deselect_ all" value=1
onClick="select All(false, 'deselect', document.imageF orm['move[]'])"> -
De-Select All</td>
</tr>
This seems to work across the board! Thanx for the tip on "onClick".
Now to figure out how to determine if the user selected an item from the
dropdown to change hasSelectedAlbu m. I can't use the "onChange" event
within the dropdown because the PHP code that generates the dropdown is
shared across multiple instances.

Phil
Phil
---------------------------------------
TCL and PHP: The Superior Web Languages!
javascript: Is Cool
ASP: why?
http://valsignalandet.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #7
JRS: In article <Gg1fc.110637$o R5.109407@pd7tw 3no>, seen in
news:comp.lang. javascript, Reply Via Newsgroup <reply-to-
ne*******@pleas e.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.java script 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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

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

Similar topics

3
4049
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
2718
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 .="<li>".$wohdate."</li>" ; $TemplateText = Replace($TemplateText,"@$wohdisplayndx@",$woh_display);
59
7055
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 http-equiv="Content-Type" content="text/html;
1
2059
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 the menu, nothing comes up. I copied the code for the page directly from the press releases page on our main site: www.maverickentertainment.cc, and it is using an identical database, so I am unsure what is wrong. The issue happens when you...
0
1689
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 errors..Not able to proceed further.(./configure went smooth). I request you to provide suggestions to solve this problem. -5.2.1/include -I/lamp/php-5.2.1/main -I/lamp/php-5.2.1 -I/usr/local/ include/libxml2 -I/lamp/php-5.2.1/ext/date/lib...
0
4796
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
25621
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 idea? Thanks you very much
0
2753
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: ext/xmlrpc/.libs/xmlrpc-epi-php.o(.text+0x359): In function `set_zval_xmlrpc_type': /php-5.2.5/ext/xmlrpc/xmlrpc-epi-php.c:1313: undefined reference to `XMLRPC_CreateValueDateTime_ISO8601'
3
4614
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 a database. My menu will be structured as follows: The first setion of the menu is hard-coded (the home and management sections). The rest of the menu will be populated from a database. The top level links (categories) will come from one table...
4
4389
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 the latest PHP, 5.2.5. The ./configure finally went through OK. Make and make install. Then I restarted Apache... but the phpinfo() shows no change. It's still using 5.1.6. When I run "php -v" from command line I get a huge error I'll include...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.