Click to See Complete Forum and Search --> : multiple checkbox problems


aussiefly
01-09-2008, 07:34 AM
hey guys!

just working on a multiple checkbox solution which also restricts the number of boxes selected and im having dramas getting the php/mysql to work. It just doesnt seem to restrict you clicking the options or popup the warning window...any ideas for a complete newb??

ok the php/form part

Code:

<?
$siz=$i;
for($i=0;$i<$siz;$i++){
?>
<INPUT TYPE=CHECKBOX VALUE="<?
print $ch["id"][$i];
?> onClick="return KeepCount()"
" name="'sel1[1]' , , 'sel1[2]' , , 'sel1[3]' ">
<?
print $ch["title"][$i];
?><BR>

<?
}
?>


And now the section of javascript it should be calling:

Code:

function KeepCount() {

var NewCount = 0

if (document.NewUser.sel1[1].checked)
{NewCount = NewCount + 1}

if (document.NewUser.sel1[2].checked)
{NewCount = NewCount + 1}

if (document.NewUser.sel1[3]checked)
{NewCount = NewCount + 1}

if (NewCount == 4)
{
alert('Form Accepts 3 Selections Maximum')
document.NewUser; return false;
}
}


Any ideas why its not working??? do i have the onclick in the wrong place??

Weedpacket
01-09-2008, 09:56 AM
This is a JavaScript problem, not a PHP one, since that is where you are doing your check (which isn't to say you shouldn't check again in PHP after the form is submitted.)

But looking at your JavaScript, I fail to see how NewCount could ever possibly equal 4.

trevorsg
01-12-2008, 10:19 PM
I am not very strong in JS, but I would do something like this:


//first of all, assign all of your checkboxes unique IDs
//I made a few more changes to the <input>
//i.e. <input type="checkbox" id="cb1" onchange="return keepCount();" value=... >

keepCount ()
{
var newCount = 0;
if (document.getElementById("cb1").checked)
newCount++;
if (document.getElementById("cb2").checked)
newCount++;
// (...)
if (newCount>3)
{window.alert("You can choose a maximum of 3"); return false; }
else {return true;}
}

After looking at your code again, I noticed you were missing a semicolon

alert('Form Accepts 3 Selections Maximum')


Like I said, I'm not very strong in JS, so please let me know if that works!

Good luck.