mishuk Posted March 26, 2007 Share Posted March 26, 2007 Hi. I am attempting to create an update form where the current fields in the table are populated into input boxes and then can be altered where necessary. I was wondering if it is possible to pre check radio buttons, check boxes and pre populate drop down menus based on the results currently in the table. eg. if the gender of a person is male then the radio button will be pre checked as male. How would this be done.? Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/ Share on other sites More sharing options...
per1os Posted March 26, 2007 Share Posted March 26, 2007 <input type="checkbox" name="gender" checked="CHECKED" /> Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215593 Share on other sites More sharing options...
DeathStar Posted March 26, 2007 Share Posted March 26, 2007 <select name="input"> if ($gender = "male"){ <option value='female'> <option value="male" selected='selected'>;} else{ <option value='male'> <option value="female" selected='selected'>;} Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215594 Share on other sites More sharing options...
mishuk Posted March 26, 2007 Author Share Posted March 26, 2007 checked="checked" is there an oposite to "checked" e.g "unchecked" Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215610 Share on other sites More sharing options...
per1os Posted March 26, 2007 Share Posted March 26, 2007 Just leave the checked="checked" out. Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215616 Share on other sites More sharing options...
mishuk Posted March 26, 2007 Author Share Posted March 26, 2007 Cheers for the help. Carrying on from this. can i dynamically place a "selected" option in this drop down menu. something like echo '<option value="' . $row['ethnicity_id'] . '" if ($ethnicity=$row['ethnicity_id'] echo "selected">' . $row['ethnicity'] . '</option>'; What would be the correct syntax? $res=mysql_query("select ethnic_id, ethnicity from tbl_ethnicity"); if(mysql_num_rows($res)==0) { echo "No Data"; } else { echo '<select name="ethnicity">'; echo '<option value = "-1">Select:</option>'; while($row=mysql_fetch_assoc($res)) { echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>'; } echo '</select>'; }; Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215667 Share on other sites More sharing options...
per1os Posted March 26, 2007 Share Posted March 26, 2007 Maybe if the "if" statement was done right. if ($ethnicity == $row['ethnicity_id']) echo "selected">' Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215670 Share on other sites More sharing options...
mishuk Posted March 26, 2007 Author Share Posted March 26, 2007 yeh sorry that was me being careless. Already tried that and get the error Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in ... on line 275 here is the full line echo '<option value="' . $row['ethnicity_id'] . '" if ($ethnicity == $row['ethnicity_id']) echo "selected">' . $row['ethnicity'] . '</option>'; Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215680 Share on other sites More sharing options...
per1os Posted March 26, 2007 Share Posted March 26, 2007 echo '<option value="' . $row['ethnicity_id'] . '" '. if ($ethnicity == $row['ethnicity_id']) echo "selected">' . $row['ethnicity'] . '</option>'; changed the . '" '. if portion. Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215682 Share on other sites More sharing options...
mishuk Posted March 26, 2007 Author Share Posted March 26, 2007 unfortunately still no joy Parse error: syntax error, unexpected T_IF Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215688 Share on other sites More sharing options...
DeathStar Posted March 26, 2007 Share Posted March 26, 2007 Please post youre complete script ehre. Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215703 Share on other sites More sharing options...
jorgep Posted March 26, 2007 Share Posted March 26, 2007 You can make it shorter like this: echo '<option value="'.$row['ethnicity_id'].'"'.($ethnicity==$row['ethnicity_id'])?"selected":"".'>'.$row['ethnicity'].'</option>'; (question)?action_if_true:action_if_false. Good Luck Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215717 Share on other sites More sharing options...
mishuk Posted March 27, 2007 Author Share Posted March 27, 2007 Still doesnt wor. Here is a bigger chunk of the script. At the beginning of the while $ethnicity is populated with the result from the student query eg $ethnicity = 1 and i want to then dynamically generate the below drop down menu with the ethnicity corresponding to the $ethnicity selected $res=mysql_query("select * from tbl_student where stud_id = $student"); while($row = mysql_fetch_array($res)) { $ethnicity = $row['ethnic_id']; ?> <FORM method="post" action="admin.php?do=editstu"> <TABLE BORDER="0" ALIGN="center"> <TR> <TD>Ethnicity:</TD> <TD> <? $res=mysql_query("select ethnic_id, ethnicity from tbl_ethnicity"); if(mysql_num_rows($res)==0) { echo "No Data"; } else { echo '<select name="ethnicity">'; echo '<option value = "-1">Select:</option>'; while($row=mysql_fetch_assoc($res)) { echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>'; } echo '</select>'; }; ?> </TD> </TR> <TR> <TD><input name="submit" type="submit" value="Update Student"></TD> <TD><input name="reset" type="reset" value="Reset"></TD> </TR> </TABLE> </FORM> Thanks Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-215768 Share on other sites More sharing options...
mishuk Posted April 18, 2007 Author Share Posted April 18, 2007 bump.. Anybody ? Link to comment https://forums.phpfreaks.com/topic/44394-values-into-radiocheck-and-drop-down-menus/#findComment-231772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.