How can see radio button was checked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluehawk2506
Forum Newbie
Posts: 2
Joined: Tue Feb 07, 2006 12:32 pm
Location: Thailand

How can see radio button was checked

Post by bluehawk2506 »

hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have 4 radios buttons that you choose from for a test on one form and then at the bottom I have a Back button and a Next button, How Do I Make It So When You Click The Back Button It See What Radio Button that you were Checked and then click the Next button it still checked. And this my code.

Code: Select all

<html>
<head>
	<title>Exam Online</title>
</head>
<body>
<div align=center>
<form action="<?=$PHP_SELF?>?Prev_Page=$Prev_Page&Next_Page=$Next_Page" method="post">
<? 
	$conn = mysql_connect( $hostname, $username, $password );
	mysql_query("SET NAMES TIS620"); 
	if ( ! $conn )
		die( "Can't connect to MySQL" );
	mysql_select_db( $dbname, $conn )
		or die ( "Can't Select the $dbname" );

	$strSQL = "select * From lesson1 ";
	
	/* Set variable for a page */
	$Per_Page =1;
	if(!$Page)
	$Page=1;
	switch ($_POST['Page'])
	{
		case 'Back' :
			$Prev_Page = $Page-1;
			break;
		case 'Next' :
			$Next_Page = $Page+1;
			break;
	}

	$result = mysql_query($strSQL);
	$Page_start = ($Per_Page*$Page)-$Per_Page;
	$Num_Rows = mysql_num_rows($result);

	if($Num_Rows<=$Per_Page)
	$Num_Pages =1;
	else if(($Num_Rows % $Per_Page)==0)
	$Num_Pages =($Num_Rows/$Per_Page) ;
	else 
	$Num_Pages =($Num_Rows/$Per_Page) +1;

	$Num_Pages = (int)$Num_Pages;

	if(($Page>$Num_Pages) || ($Page<0))
	print "<center><b>&#3592;&#3635;&#3609;&#3623;&#3609; $Page &#3617;&#3634;&#3585;&#3585;&#3623;&#3656;&#3634; $Num_Pages &#3618;&#3633;&#3591;&#3652;&#3617;&#3656;&#3617;&#3637;&#3586;&#3657;&#3629;&#3588;&#3623;&#3634;&#3617;<b></center>";
	$strSQL .= " Where 1 Order by Number LIMIT $Page_start , $Per_Page";
	
	$result = mysql_query($strSQL);

	While($rs= mysql_fetch_array($result))
	{
		echo "<table cellpadding=\"4\" cellspacing=\"3\" border=\"1\" border width= \"65%\" align=\"center\" bgcolor=\"lavender\">
				   <tr>
				   <td>$rs[Number]."." $rs[Question]</td>
				   </tr>\n";
?>
		<td><input type="radio" name="C$rs[0]" value="Choice1"> <?echo $rs[Choice1];?></td>
		</tr>
		<td><input type="radio" name="C$rs[0]" value="Choice2"> <?echo $rs[Choice2];?></td>
		</tr>
		<td><input type="radio" name="C$rs[0]" value="Choice3"> <?echo $rs[Choice3];?></td>
		</tr>
		<td><input type="radio" name="C$rs[0]" value="Choice4"> <?echo $rs[Choice4];?></td>
		</tr>
<?
		echo "<td align=\"center\"><img src=$rs[Pictures]></td>\n
		</tr>\n
		</table>";
	}
	echo "<table cellpadding=\"4\" cellspacing=\"3\" border=\"0\" border width= \"65%\" align=\"center\" bgcolor=\"lavender\">\n";
	/* Back Button */
	if($Prev_Page) 
?>
	<td align="left"><input type="submit" name="Page" value="Back"></td>
	
<? /* Next Button */
	if($Page!=$Num_Pages)
?>
	<td align="right"><input type="submit" name="Page" value="Next"></td>
<? 
	echo "</table>";
	mysql_close( $conn );
?>
</div>
</form>
</body>
</html>
:cry:


hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$_POST['C$rs[0]'] will have your information.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

on submission, when you use $_POST["C$rs[0]"], it will display a value which would from choice1 to choice4 as feyd said. Incase of check boxes, you will receive a set of values from {choice1,..,choice4}.

I am just curious to know why the name of the following INPUT Radion control is C$rs[0]

Code: Select all

input type="radio" name="C$rs[0]" value="Choice4"> <?echo $rs[Choice4];?></td>
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

something like

Code: Select all

<input type="radio" name="test" value="1" 
<? if (isset($POST['var'])) { echo"checked"; } ?> >
bluehawk2506
Forum Newbie
Posts: 2
Joined: Tue Feb 07, 2006 12:32 pm
Location: Thailand

Post by bluehawk2506 »

I'm sorry that my question is not clear. My program is examination that one question per page and have 4 choices when you choose some radio button then click the Next button for to do another question. And if you want to undo the previous question by click the Back button for correct the radio button that you choose but I want my program to show the radio button that you were choose it still checked. And when you click the Next button the radio button is still checked.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

bluehawk2506 wrote:I'm sorry that my question is not clear. My program is examination that one question per page and have 4 choices when you choose some radio button then click the Next button for to do another question. And if you want to undo the previous question by click the Back button for correct the radio button that you choose but I want my program to show the radio button that you were choose it still checked. And when you click the Next button the radio button is still checked.
what is your problem now and thanks for explaining the app. If you want to enable back option, you can better store an answer in a session variable once you move from one question to other and you can use this later on to restore the chosen answer else you can retrieve the stored answer in the DB.
Post Reply