Hi,

How do i check if any of these checkboxs are selected. If nothing is selected then invalidate the form. Javascript

thanks

<input type="checkbox" name="MYcheckbox[]" value="0">
<input type="checkbox" name="MYcheckbox[]" value="1">
<input type="checkbox" name="MYcheckbox[]" value="2">
<input type="checkbox" name="MYcheckbox[]" value="3">

You can iterate trough forum controls with Javascript

var i;
for(i=1;i<18;i++) {
      document.formPermissions.elements[i].checked = 1;
}

I couldn't do it. I generate those checkboxes by php and store values in them. name="MYcheckbox[]".
As i am a new person in jScript, i need help.

Thanks

What you need to do in PHP is give each checkbox an individual ID then,
as in:

echo "<input type='checkbox' id='checkbox".$i."' name='".$checkboxName."'>&nbsp;</input>";

Now you can iterate trough them in javascript by doing this:

var i;
var isItChecked;
for(i=0;i<9;i++) {
   isItChecked = document.myFormName.elements[i].checked;

}

Hope this helps ^^
brecht

Ok i'll try that way. Thank you

Only when there are more than one DOM objects with the same ID, the DOM treats that as an array, it says in a web side. This is exactly what i try to do, but i couldn't find any.

I should stop writing my posts so late, anyway
Looking back at my previous post I don't even think you HAVE to give them a unique ID to be able to iterate trough them. ID is there to give each element a unique identifier though, I cannot locate that site you talk of, maybe you can point me to it so I can read up on that. Anyway, if you could give me a piece of code I could try and help with that, see where that goes.

I want to add a javascript to check if any checkbox is selected or not.
Thanks

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="save.php">
<table>
	<tr>
		<td>ID</td>
		<td>COUNTRY</td>
		<td>Unit</td>
		<td>&nbsp;</td>
	</tr>
<?php
$connection=mysql_connect("localhost", "root", "");
if ($connection) {
	mysql_select_db("phptest", $connection);
} else {
	echo "Database connection was failed";
	exit();
}	

$sql="SELECT id, name FROM country_1";
$runSql=mysql_query($sql);

if (@mysql_num_rows($runSql)>0) {
	while ($newArray=mysql_fetch_array($runSql)) {
?>
	<tr>
		<td><?php echo $newArray['id']; ?></td>
		<td><?php echo $newArray['name']; ?></td>
		<td><input type="textbox" name="textboxUnit[<?php echo $newArray['id']; ?>]" value=""></td>
		<td><input type="checkbox" name="checkboxCountry[<?php echo $newArray['id']; ?>]" value="<?php echo $newArray['id']; ?>"></td>
	</tr>
<?php
	}
}
?>
</table>
</form>
</body>
</html>
onclick="if(this.checked == 1) { alert('checked: '+<? echo $newArray['id'];?>); }"

Hopefully this is what you want : - )

Sorry that is my mistake. I meant to put a submit button to validate checkboxes in a javascript fuction. If anything is checked then return true, otherwise return false.

Thanks

function checkCheckBoxes() {
    var i;
    var isOneChecked = false;
    for(i=0;i<5;i++) {
      if(checkBoxCountry[i].checked) {
        isOneChecked = true;
      }
    }
    return isOneChecked;
  }
<input type="submit" onclick="alert(checkCheckBoxes())" value="Submit">

This should work. If you want to make it dynamic you can do that with PHP.

Didn't work. Can we not modify this one?
Thanks

function checkCheckBoxes() {    
	var counter;
	var selectedCheckbox = 0;
	
	for (counter = 0; counter < form1.checkboxCountry.length; counter++) {
		if (form1.checkboxCountry[counter].checked) { 
			selectedCheckbox = selectedCheckbox + 1; 
		}
	}
	if (selectedCheckbox == 0 ) {
		alert("Please make selections.");
		return (false);
	}
	}
<form name="form1" method="post" action="save.php" onSubmit="return checkCheckBoxes();">
.......
<input type="checkbox" name="checkboxCountry[<?php echo $['id']; ?>]" value="<?php echo $new['id']; ?>">

Excuse me, my previous post was indeed nonsense. This on the other hand should work:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<script type="text/javascript">
			function checkCheckBoxes(formName) {
			    var i;
			    var numberOfChecked = 0;
				
			    for(i=0;i<5;i++) {
					if(formName.elements['checkBoxCountry['+i+']'].checked) {
						numberOfChecked++;
					}
			    }
			    return numberOfChecked;
			  }
		</script>
	</head>
	
	<body>
		<form name="myForm">
			<input type="checkbox" name="checkBoxCountry[0]"></input>
			<input type="checkbox" name="checkBoxCountry[1]"></input>
			<input type="checkbox" name="checkBoxCountry[2]"></input>
			<input type="checkbox" name="checkBoxCountry[3]"></input>
			<input type="checkbox" name="checkBoxCountry[4]"></input>
			<input type="submit" onclick="alert('Number of checked checkBoxes: ' + checkCheckBoxes(myForm))" value="Submit">
		</form>
	</body>
</html>

Good luck

hi friends i need one cgi example program pls send me

Hi brechtraj,

I works fine in html but, as long as i use it php, it doesn't work. Do you think i do mistake below?

thanks

<input type="checkbox" name="checkboxDicode[<?php echo $new['studentId']; ?>]" value="<?php echo $new['studentId']; ?>">

That depends on what's in $new. Is it a number? I'm guessing it is, and how many do you have, in my example there are 5 checkboxes in the form. If your studentId's follow each other up like this:
1, 2, 3
and not like:
3, 34, 55, 56, 57

You can probably change this Javascript:

function checkCheckBoxes(formName) {
			    var i;
			    var numberOfChecked = 0;
				
			    for(i=0;i<5;i++) {
					if(formName.elements['checkBoxCountry['+i+']'].checked) {
						numberOfChecked++;
					}
			    }
			    return numberOfChecked;
			  }

to this:

function checkCheckBoxes(formName) {
			    var i = 0;
			    var numberOfChecked = 0;
				
			    while(formName.elements['checkBoxCountry['+i+']']) {
					if(formName.elements['checkBoxCountry['+i+']'].checked) {
						numberOfChecked++;
					}
					i++;
			    }
			    return numberOfChecked;
			  }

EDIT: code tested and should work
Hope this helps you ^^

Yes you are rigth. checkBoxCountry has unknown numbers and strings. An example below. How can we do it?

Thanks

<input type="checkbox" name="checkBoxCountry[01SW]"></input>
<input type="checkbox" name="checkBoxCountry[A22]"></input>
<input type="checkbox" name="checkBoxCountry[BX2L]"></input>
<input type="checkbox" name="checkBoxCountry[11OX]"></input>
<input type="checkbox" name="checkBoxCountry[41W]"></input>

What you could do is give each checkbox an ID and go by that ID instead of the name, then you can get the name of that checkbox because you have it's ID:

<input type="checkbox" id="checkBox1" name="checkBoxCountry[01SW]"></input>
<input type="checkbox" id="checkBox2" name="checkBoxCountry[A22]"></input>
<input type="checkbox" id="checkBox3" name="checkBoxCountry[BX2L]"></input>
<input type="checkbox" id="checkBox4" name="checkBoxCountry[11OX]"></input>
<input type="checkbox" id="checkBox5" name="checkBoxCountry[41W]"></input>

If you have any questions just reply, I don't have much time to react now I'm sorry

Brectjah,
I think i have to buy a book and study JavaScript. It is solved by all your help. Thank you very much.

<script type="text/javascript">
function validate() {
var count=this.form2.elements;
var i;
var selectedCheckbox = 0;
   for(i=0;i<count.length;i++) {
	if(document.form2.elements[i].checked) {
		selectedCheckbox = selectedCheckbox + 1; 
	}
   }
   if (selectedCheckbox == 0 ) {
	alert ("Please select at least one student");
	return false;
   }

return true;
}

<form name="form2" action="s.html" onSubmit="return validate();">
<input type="checkbox" id="ch0" name="checkBoxCountry[0]"></input>
<input type="checkbox" id="ch1" name="checkBoxCountry[122]"></input>
<input type="checkbox" id="ch2" name="checkBoxCountry[2sx]"></input>
<input type="checkbox" id="ch3" name="checkBoxCountry[3ww]"></input>
<input type="checkbox" id="ch4" name="checkBoxCountry[41]"></input>
<input type="submit" value="Submit">
</form>

Great to hear that! :)
Good luck on learning Javascript

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.