0

Hey, so I've tried like 50 different guides and so on with how to validate this form, and so far nothing works.

When the user hits submit, I would very much like to validate if a button in each row has been selected, ideally it would be nice to have an indication of which row has not been answered.

Among what I tried, was to include this javascript code

<script type="text/javascript">

function valbutton(thisform) {
myOption = -1;
for (i=thisform.group1.length-1; i > -1; i--) {
if (thisform.group1[i].checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
alert("You must select a radio button");
return false;
}

alert("You selected button number " + myOption
+ " which has a value of "
+ thisform.group1[myOption].value);

// place any other field validations that you require here
thisform.submit(); // this line submits the form after validation
}
</script>

and change the submit button to

<input type="button" name="submitit" onclick="valbutton(kupon)" value="Validate" />

HTML form

<?php
require('db.php');


?>
<html>
<FORM name ="kupon" method ="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1">
	<tr>
		<td align="center">1</td>		<td align="center">X</td> 		<td align="center">2</td> 		
	</tr>
	<tr>
	

		<td align="center"><Input type = 'Radio' Name ='group1' value= '1' <?php if (isset($_POST['1']) && isset($_POST['1']) && $_POST['1'] == '1') echo 'checked="checked" '; ?></td>	
		<td align="center"><Input type = 'Radio' Name ='group1' value= 'X' <?php if (isset($_POST['1']) && isset($_POST['1']) && $_POST['1'] == 'X') echo 'checked="checked" '; ?>></td> 	
		<td align="center"><Input type = 'Radio' Name ='group1' value= '2' <?php if (isset($_POST['1']) && isset($_POST['1']) && $_POST['1'] == '2') echo 'checked="checked" '; ?>></td> 	

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='2' value= '1' <?php if (isset($_POST['2']) && $_POST['2'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='2' value= 'X' <?php if (isset($_POST['2']) && $_POST['2'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='2' value= '2' <?php if (isset($_POST['2']) && $_POST['2'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='3' value= '1' <?php if (isset($_POST['3']) && $_POST['3'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='3' value= 'X' <?php if (isset($_POST['3']) && $_POST['3'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='3' value= '2' <?php if (isset($_POST['3']) && $_POST['3'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='4' value= '1' <?php if (isset($_POST['4']) && $_POST['4'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='4' value= 'X' <?php if (isset($_POST['4']) && $_POST['4'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='4' value= '2' <?php if (isset($_POST['4']) && $_POST['4'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='5' value= '1' <?php if (isset($_POST['5']) && $_POST['5'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='5' value= 'X' <?php if (isset($_POST['5']) && $_POST['5'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='5' value= '2' <?php if (isset($_POST['5']) && $_POST['5'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='6' value= '1' <?php if (isset($_POST['6']) && $_POST['6'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='6' value= 'X' <?php if (isset($_POST['6']) && $_POST['6'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='6' value= '2' <?php if (isset($_POST['6']) && $_POST['6'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='7' value= '1' <?php if (isset($_POST['7']) && $_POST['7'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='7' value= 'X' <?php if (isset($_POST['7']) && $_POST['7'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='7' value= '2' <?php if (isset($_POST['7']) && $_POST['7'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='8' value= '1' <?php if (isset($_POST['8']) && $_POST['8'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='8' value= 'X' <?php if (isset($_POST['8']) && $_POST['8'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='8' value= '2' <?php if (isset($_POST['8']) && $_POST['8'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='9' value= '1' <?php if (isset($_POST['9']) && $_POST['9'] == '1') echo 'checked="checked" '; ?>></td>		
		<td align="center"><Input type = 'Radio' Name ='9' value= 'X' <?php if (isset($_POST['9']) && $_POST['9'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='9' value= '2' <?php if (isset($_POST['9']) && $_POST['9'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='10' value= '1' <?php if (isset($_POST['10']) && $_POST['10'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='10' value= 'X' <?php if (isset($_POST['10']) && $_POST['10'] == 'X') echo 'checked="checked" '; ?>></td> 
		<td align="center"><Input type = 'Radio' Name ='10' value= '2' <?php if (isset($_POST['10']) && $_POST['10'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='11' value= '1' <?php if (isset($_POST['11']) && $_POST['11'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='11' value= 'X' <?php if (isset($_POST['11']) && $_POST['11'] == 'X') echo 'checked="checked" '; ?>></td> 	
		<td align="center"><Input type = 'Radio' Name ='11' value= '2' <?php if (isset($_POST['11']) && $_POST['11'] == '2') echo 'checked="checked" '; ?>></td> 

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='12' value= '1' <?php if (isset($_POST['12']) && $_POST['12'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='12' value= 'X' <?php if (isset($_POST['12']) && $_POST['12'] == 'X') echo 'checked="checked" '; ?>></td> 	
		<td align="center"><Input type = 'Radio' Name ='12' value= '2' <?php if (isset($_POST['12']) && $_POST['12'] == '2') echo 'checked="checked" '; ?>></td> 	

	</tr>
	<tr>
		<td align="center"><Input type = 'Radio' Name ='13' value= '1' <?php if (isset($_POST['13']) && $_POST['13'] == '1') echo 'checked="checked" '; ?>></td>	
		<td align="center"><Input type = 'Radio' Name ='13' value= 'X' <?php if (isset($_POST['13']) && $_POST['13'] == 'X') echo 'checked="checked" '; ?>></td> 	
		<td align="center"><Input type = 'Radio' Name ='13' value= '2' <?php if (isset($_POST['13']) && $_POST['13'] == '2') echo 'checked="checked" '; ?>></td> 	

	</tr>
	
</table>
<Input type = "Submit" Name = "Submit" VALUE = "Submit"
<input type="reset" value="clear"><br>
</FORM>

</html>
<?php 

?>

I realize the javascript validation code only attempts to validate the first line, but it doesnt work with just the first line, so no use in extending it until it works. Any ideas on how I get this to work?

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Tommy.b
0

Hm, unsure how to delete or even edit my post.

I've solved it myself.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.