Forum Moderators: open
Anyway, I am trying to create a form where people have to select a radio button from 2 different tables. The tables are built via PHP, populated with info from an SQL Database.
If there is only one selection available, I have PHP place a "Checked", in the input tag of the radio button, so the user does not have to select it (since there IS only one).
The problem I have is that the code I have seems to work only when there are more than one selection. If I provide the user with the "checked" radio button, the script seems to think that nothing from that table has been selected. If I were to throw another row on the first table, and remove the "Checked" settings, everything works fine.
Here is the code I have:
<html>
<head>
<script language="JavaScript">
<!--
function radio_button_checker()
{
// set var radio_choice to false
var radio_choice_user = false;
var radio_choice_mgr = false;
// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < user_select.user.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (user_select.user[counter].checked)
radio_choice_user = true;
}
for (counter = 0; counter < user_select.mgr.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (user_select.mgr[counter].checked)
radio_choice_mgr = true;
}
if (!radio_choice_mgr ¦¦ !radio_choice_user)
{
// If there were no selections made display an alert box
alert("Both a User and Manager must be selected.")
return (false);
}
return (true);
}
-->
</script>
</head>
<body>
<form method='post' name='user_select' onsubmit='return radio_button_checker()' action='index.php'>
<table class='results' border='1' cellpadding='1' cellspacing='1'><tr>
<th>Select</th><th>Name</th><th>Site</th><th>Email Address</th><th>Phone Number</th></tr>
<tr class='col1'><td><input type='radio' name='user' value='001' checked></td>
<td>Name</td>
<td>site</td>
<td>email</td>
<td>phone</td>
</tr>
</table><br/><br><form method='post' name='user_select' onsubmit='return radio_button_checker()' action='index.php'>
<table class='results' border='1' cellpadding='1' cellspacing='1'><tr>
<th>Select</th><th>Name</th><th>Site</th><th>Email Address</th><th>Phone Number</th></tr>
<tr class='col1'><td><input type='radio' name='mgr' value='002'></td>
<td>Name</td>
<td>site</td>
<td>email</td>
<td>phone</td>
</tr>
<tr class='col2'><td><input type='radio' name='mgr' value='003'></td>
<td>Name</td>
<td>site</td>
<td>email</td>
<td>phone</td>
</tr>
</table><br/><input type='submit' name='user_selected' value='Submit'>
</form>
Any help would be appreciated.
You might be able to do something like this:
<script type="text/javascript">
function radio_button_checker() {
// set var radio_choice to false
var radio_choice_user = false;
var radio_choice_mgr = false;
// Loop from zero to the one minus the number of radio button selections
var userList = document.getElementsByName('user');
var mgrList = document.getElementsByName('mgr');
for (counter = 0; counter < userList.length; counter++) {
// If a radio button has been selected it will return true
// (If not it will return false)
if (userList[counter].checked) {
radio_choice_user = true;
}
}
for (counter = 0; counter < mgrList.length; counter++) {
// If a radio button has been selected it will return true
// (If not it will return false)
if (mgrList[counter].checked) {
radio_choice_mgr = true;
}
}
if (!radio_choice_mgr ¦¦ !radio_choice_user) {
// If there were no selections made display an alert box
alert("Both a User and Manager must be selected.")
return (false);
}
return (true);
}
</script>
Also, note that I removed your language attribute (it's invalid and not needed) and replaced it with type (which is required to be valid), and I removed your HTML comments at the beginning and end of your script tags (this has not been needed for many years and should no longer be used).
Hope this helps.