Jump to content

Initializing Form Radio Checked Based on Variable


OtagoHarbour
 Share

Recommended Posts

I have a set of radio buttons in a form and would like to initialize which button is checked based upon a session variable. The code I tried was as follows.

<table border="1">	<tr>	<th align="center" colspan="2" style="background:orange">Look Up Table</th>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="GREY" value="GREY" onclick="EnableNonlinear()" 		checked='<?php echo ($_SESSION['LUT']=="GREY") ?>'/>Gray</td>	<td><input type="radio" name="LUT" id="HOS" value="HOS" onclick="EnableNonlinear()" />Heated Object</td>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="RAINBOW" value="RAINBOW" onclick="EnableNonlinear()" />Rainbow</td>	<td><input type="radio" name="LUT" id="ARCTIC" value="ARCTIC" onclick="EnableNonlinear()" />Arctic</td><table>

But the "Gray" button is checked despite the fact that $_SESSION['LUT']!="GREY".Another approach would be to use a DOM function as shown here http://www.w3schools.com/jsref/prop_radio_checked.asp. However this example calls the check() and uncheck() functions based upon an event. There does not appear to be an initialize event for tables. Does <table> have an option for calling an initialization function?Many thanks in advance for any help,Peter.

Link to comment
Share on other sites

I'm not exactly sure what you are trying to accomplish with that line per se, but the checked property requires a true/false value.http://www.w3schools.com/jsref/prop_checkbox_checked.aspA more practical application of this functionality would be to do something like this; which is to usually do all your PHP work at the beginning of the document; i.e. do calculations, set flags, etc and then use the resulting variables simply throughout the rest of the page. so, something like this:page.php

<?php  $lutCheckedStatus = false;    if($_SESSION['LUT'] == "GREY"){	$lutCheckedStatus = true;  };?><table border="1">	<tr>	<th align="center" colspan="2" style="background:orange">Look Up Table</th>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="GREY" value="GREY" onclick="EnableNonlinear()" 		checked='<?php echo $lutCheckedStatus ?>'/>Gray</td>	<td><input type="radio" name="LUT" id="HOS" value="HOS" onclick="EnableNonlinear()" />Heated Object</td>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="RAINBOW" value="RAINBOW" onclick="EnableNonlinear()" />Rainbow</td>	<td><input type="radio" name="LUT" id="ARCTIC" value="ARCTIC" onclick="EnableNonlinear()" />Arctic</td><table>

Link to comment
Share on other sites

I'm not exactly sure what you are trying to accomplish with that line per se, but the checked property requires a true/false value.http://www.w3schools.com/jsref/prop_checkbox_checked.aspA more practical application of this functionality would be to do something like this; which is to usually do all your PHP work at the beginning of the document; i.e. do calculations, set flags, etc and then use the resulting variables simply throughout the rest of the page. so, something like this:page.php
<?php  $lutCheckedStatus = false;    if($_SESSION['LUT'] == "GREY"){	$lutCheckedStatus = true;  };?><table border="1">	<tr>	<th align="center" colspan="2" style="background:orange">Look Up Table</th>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="GREY" value="GREY" onclick="EnableNonlinear()" 		checked='<?php echo $lutCheckedStatus ?>'/>Gray</td>	<td><input type="radio" name="LUT" id="HOS" value="HOS" onclick="EnableNonlinear()" />Heated Object</td>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="RAINBOW" value="RAINBOW" onclick="EnableNonlinear()" />Rainbow</td>	<td><input type="radio" name="LUT" id="ARCTIC" value="ARCTIC" onclick="EnableNonlinear()" />Arctic</td><table>

Thank you for your help. I ended up taking that general approach but I found that the last radio button that had checked in it was checked even if I set checked=false. However I found that this works.
<?php   	$greyLUT=($_SESSION['LUT']=='GREY')? "checked" : " ";   	$hosLUT=($_SESSION['LUT']=='HOS')? "checked" : " ";   	$raindowLUT=($_SESSION['LUT']=='RAINBOW')? "checked" : " ";   	$arcticLUT=($_SESSION['LUT']=='ARCTIC')? "checked" : " ";   	$fireLUT=($_SESSION['LUT']=='FIRE')? "checked" : " ";?><table border="1">	<tr>	<th align="center" colspan="2" style="background:orange">Look Up Table</th>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="GREY" value="GREY" onclick="EnableNonlinear()" 		<?php echo $greyLUT ?>/>Gray</td>	<td><input type="radio" name="LUT" id="HOS" value="HOS" onclick="EnableNonlinear()" 		<?php echo $hosLUT ?>/>Heated Object</td>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="RAINBOW" value="RAINBOW" onclick="EnableNonlinear()"  		<?php echo $raindowLUT ?>/>Rainbow</td>	<td><input type="radio" name="LUT" id="ARCTIC" value="ARCTIC" onclick="EnableNonlinear()"  		<?php echo $arcticLUT ?>/>Arctic</td>	</tr>	<tr align="left">	<td><input type="radio" name="LUT" id="FIRE" value="FIRE" onclick="EnableNonlinear()"   		<?php echo $fireLUT ?>/>Fire</td></table>

Thanks again for your help,Peter.

Link to comment
Share on other sites

my bad. it is correct that the HTML syntax should be

checked="checked"

however, for dealing with JS/HTML DOM directly, it should be true/false.

Edited by thescientist
Link to comment
Share on other sites

my bad. it is correct that the HTML syntax should be
checked="checked"

however, for dealing with JS/HTML DOM directly, it should be true/false.

I was thinking of using DOM but it seems that the associated functions are event driven. I could not see where to call a function to initialize the table.Thanks,Peter.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...