Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox and array problem...

Status
Not open for further replies.

Nematoth

Programmer
Mar 29, 2002
48
MY
I've tried searchign for the answer to this already but I just can't figure it out I hope someone can help.

I have a set of four checkboxes and I want one of them to be selected before the form is submitted. Now this sounds simple I know.. but I can't get it to work. Here's the code for the form:
Code:
<form name="addbook" method="POST" action="<?=$sFileName?>">

<input type="checkbox" name="bookid[]" value="<?=$flda_bookid?>" class="blacktext01">
<input type="hidden" name="FormAction" value="addphone">
<input type="hidden" name="FormName" value="addbook">
<input type="hidden" name="forwardURL" value="recip_but.php">
<a href="#" target="_self" onclick="checkForm();"><img src="images/but_add01.gif" width="85" height="39" border="0"></a>
</form>

And the code on the button:

Code:
function checkForm()
{
	var noChecked =0;
	for(var i = 0; i < document.addbook.elements.length; i++)
	{
		if(document.addbook.elements.checked)
		{
			noChecked++;
		}
	}
	if (noChecked !=1) 
	{
		alert("Please select ONE name then click Add. Number of checkboxes checked:"+noChecked);
		//alert("Please select ONE name then click Add/n/n"+document.addbook.elements.length);
		return false;
	}	
	else 
	{
		alert("Submitting form");
		document.addbook.submit();
	}
}
</script>

I've tried all sorts of things and this is where the code currently stands at.. I'm am sureit is something simple..

Basically I need to check if the checkbox array, bookid[] is checked or not..

Many thanks to anyone who can help..

Nematoth
 
Hi Nematoth,

I see only one checkbox in the form (bookid[])

Try "if (document.addbooks.elements.checked)"

If you want exactly one book checked, why not use a radiogroup? Initially have a dummy option (first option) in the group checked (saying "Notting selected" or something) and then (on form submit) check if this dummy is still selected: if so alert, else submit.

Hope that helps,

Boelem

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top