View Full Version : make sure checkbox is selected before sending
sweenster
12-17-2002, 11:23 PM
as the title says i have a simple voting script with a checkbox. Tick option 1 to vote for one thing, and option 2 for a second thing.
I need a javascript that can check that one of the boxes has been ticked before it submits the form.
quite simple..., any suggestions??:)
chrismiceli
12-17-2002, 11:33 PM
use a for loop
for (var i = 0; i < 2; i++) {
if (document.formname.elements[i].selected == true) test = true;
}
// only if the select boxes are first in the elements, you could put them in their own form if not. then do this on the submit
if (test) {
//code
}
else {
alert("please select one");
}
sweenster
12-18-2002, 12:29 AM
still a bit confused by this. I downloaded a similar one but couldn't really figure how to actually send the data.
Tell u what... here's my existing code. how would u implement it?
<form action="popupwin.php" method="GET">
<input type="hidden" name="page" value="armvote">
<input type="hidden" name="id" value="<? print(''.$id.''); ?>">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td height="10" colspan="2"> </td></tr>
<?
#<tr><td colspan="2">pic<br>in<br>here</td></tr>
#<tr><td height="10" colspan="2"> </td></tr>
?>
<tr><td colspan="2"><? print(''.$text.''); ?></td></tr>
<tr><td height="10" colspan="2"> </td></tr>
<tr><td><input type="radio" name="response" value="1" selected="no"></td><td><? print(''.$na1.''); ?></td></tr>
<tr><td height="10" colspan="2"> </td></tr>
<tr><td><input type="radio" name="response" value="2" selected="no"></td><td><? print(''.$na2.''); ?></td></tr>
<tr><td height="10" colspan="2"> </td></tr>
<tr><td colspan="2"><input type="Submit" name="Submit" value="Vote Now" class="FORM"></td></tr>
<tr><td height="10" colspan="2"> </td></tr>
</table>
</form>
whammy
12-18-2002, 01:33 AM
<form id="blah" action="popupwin.php" method="GET" onsubmit="return validate(this.form)">
function validate(f)
var radios = f.response;
var radiolength = f.response.length;
var checkflag = false;
var i = 0;
while (i < radiolength && !checkflag) {
if (radios[i].checked) checkflag = true;
i++
}
return checkflag;
}
Haven't tested it.... ;)
sweenster
12-18-2002, 10:27 PM
erm.... I know you haven't tested it.. cos it aint working.
I've not put the script in the header tag but just in a <script></script> tag...
whammy
12-18-2002, 11:33 PM
Oops, my bad... should have only used (this) and I left out a bracket... try this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title></title>
<script type="text/javascript">
<!--
function validate(f) {
var radios = f.response;
var radiolength = radios.length;
var checkflag = false;
var i = 0;
while (i < radiolength && !checkflag) {
if (radios[i].checked) checkflag = true;
i++
}
return checkflag;
}
// -->
</script>
</head>
<body>
<div>
<form id="ijnuhbygv" action="popupwin.htm" onsubmit="return validate(this)">
<input type="radio" name="response" value="1" />1<br />
<input type="radio" name="response" value="2" />2<br />
<input type="submit" value="Vote Now!" />
</form>
</div>
</body>
</html>
You'll need to add in your php code and change the action to ".php"... :)
sweenster
12-19-2002, 11:07 PM
young man, you are a genius.
:p :thumbsup: :cool:
whammy
12-19-2002, 11:40 PM
Thanks for the compliment... "young man", that is (thanks for the other one too!). ;)
Judging by the birthday in your profile, I'm 10 years older than you. :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.