Need help with checking radio buttons
Posted: Wed Jan 13, 2010 2:52 am
I have a html page that lets the user pick one of two radio buttons :
The javascript that does the checking :
The above code works fine, but the processing page is php, and so the html page should be like the example below if i want to access the post value from $_POST['btn'] as an array.
(example 2)
What should my javascript look like if i want to check whether either of the radio buttons have been checked within the above (example 2) html code?
Code: Select all
<html>
<form action="somepage.php" method="post" name="form1" onSubmit="return checkIfRadioIsTicked();"; />
Input : <input type="radio" name="btn" value="1" />
Input1 : <input type="radio" name="btn" value="2" />
<input type="submit" value="Continue" name="button" />
</form>
</html>
Code: Select all
function checkIfRadioIsTicked()
{
if ( document.form1.btn[0].checked == false || document.form1.btn[1].checked == false )
{
alert('Not checked');
return false;
}
else {
return true;
}
}
(example 2)
Code: Select all
<html>
<form action="somepage.php" method="post" name="form1" onSubmit="return checkIfRadioIsTicked();"; />
Input : <input type="radio" name="btn[]" value="1" />
Input1 : <input type="radio" name="btn[]" value="2" />
<input type="submit" value="Continue" name="button" />
</form>
</html>