|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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! |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > Checkboxes: allowing only 4 to be selected |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|