HTML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignHTML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 19th, 2000, 04:47 PM
terium terium is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 60 terium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to terium
I have like 30 different checkboxes with the name products[] (for PHP purposes). What I need is that whenever someone selects a checkbox javascript pops a message and unchecks it if there are already four selected. How would I go about doing that?

I suppose what could be done is this: Whenever one is checked, add 1 to a variable. If one is unchecked, subtract 1. If variable+1=5, then pop up the message and uncheck it. However, I don't know javascript (only c++/php)... Thanks

Reply With Quote
  #2  
Old July 19th, 2000, 05:23 PM
billyo billyo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 114 billyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
This is off the top of my head, but I think you're on the right track. I understand why you have to name your checkboxes with the [], for PHP. (If you figure out how to make PHP recognize which ones are unchecked and which ones aren't let me know. As far as I can tell, if you have a control array of thirty checkboxes on your form and only four are checked then PHP will only create an array of those four. For example: JS_Array[12]=checked, JS_Array[25]=checked, then PHP_array[0]=checked, PHP_array[1]=checked. With no way to tell what the original indices of the JS array were.) Anyway, here's some code:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
<html>
<head>
<script language="JavaScript"><!--
var count=0;
function MONITOR(obj)
{
if (obj.checked==true)
{
count+=1;
if(count>4)
{
alert("yo, you checked too many")
obj.checked=false
}
}
if (obj.checked==false)
{
count-=1
}
}
//--></script>
<style><!--
body
{
font-size:12px;
font-family:Arial;
}
--></style>
<title>New Page 1</title>
</head>
<body>
<form>
<table>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
<tr><td><input type=checkbox name=phpthing[] onclick=MONITOR(this)></td></tr>
</table>
</form>
</body>
</html>
[/code]
That oughter work for ya.

Reply With Quote
  #3  
Old July 19th, 2000, 06:47 PM
terium terium is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 60 terium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to terium
Actually, since I don't specify a number (JS_Array[123]) but rather use just plain JS_Array[], it'll set JS_Array[0] to the first checked, JS_Array[1] to the second, etc.. therefore, if I want to know how many are checked I just use sizeof(JS_Array). Now, this is what I use (it's a pain) to find out which is checked:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
<?php
$value="something";
for($i = 0; $i <= sizeof($chechboxesname); $i++) {
$checkboxesname[$i] == $value ? $check = " CHECKED": $check = "";
if ($check == " CHECKED") {
break;
}
}
echo "<input type="checkbox" name="checkboxesname[]" value="$value"$check>";
?>
[/code]

I'll try your javascript now and tell you how it goes.

Reply With Quote
  #4  
Old July 19th, 2000, 07:13 PM
terium terium is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 60 terium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to terium
The script worked like a wonder! However, I have a question (not relative to the functionality apparently, but for my own curiosity). Sometimes you put ; at the end of a line:
var count=0;
count+=1;

And sometimes you don't:
alert("yo, you checked too many")
obj.checked=false
count-=1

How is it supposed to be? In C++/PHP you put a semicolon at the end, and it wouldn't work without it. Thanks for your help!

Reply With Quote
  #5  
Old July 19th, 2000, 07:29 PM
billyo billyo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 114 billyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Yeah, the semi-colon inconsistency is just me being sloppy. I don't think you need it, but it doesn't hurt and it's definitely needed in the other languages so I try to use it...
Thanks for the PHP tip, it actually cleared a few things up for me.

Reply With Quote
  #6  
Old July 20th, 2000, 07:49 AM
terium terium is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 60 terium User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to terium
Well, the script worked, so I figured it wasn't necessary. I think I'll just use it always, though, since that's what I'm used to...

Nelson

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignHTML Programming > Checkboxes: allowing only 4 to be selected


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway