vinaip Posted March 23, 2007 Share Posted March 23, 2007 Hi, I have saved a "Y" or "N" in the database field, based on a Checkbox setting in the "Add Contact" screen. Now, in the Edit screen, I want to show the same checkbox, but it should be checked or unchecked, based on the value from the database. How do I check the checkbox based on the edit screen, based on the database value, in PHP? Please help. Thanks, Vinai Link to comment https://forums.phpfreaks.com/topic/43915-how-to-check-a-checkbox-based-on-database-value/ Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 $checked = (($dbval == 'Y')?"CHECKED":""; print '<input type=checkbox name=cbox value=y ' . $checked . '/>'; Link to comment https://forums.phpfreaks.com/topic/43915-how-to-check-a-checkbox-based-on-database-value/#findComment-213211 Share on other sites More sharing options...
TecBrat Posted March 23, 2007 Share Posted March 23, 2007 I usually code with the more verbose if() statement, and I put PHP inside HTML more than I echo HTML, but there are times for both. Here's another example that does the same thing. <? if ($dbval=="Y"){$fieldnamechecked="checked"} ?> <input type=checkbox name=fieldname <? echo($fieldnamechecked); ?> value=y > Link to comment https://forums.phpfreaks.com/topic/43915-how-to-check-a-checkbox-based-on-database-value/#findComment-213218 Share on other sites More sharing options...
emehrkay Posted March 23, 2007 Share Posted March 23, 2007 im modifying frost's code $checked = ($dbval == 'Y') ? 'checked="checked"' : ''; echo "<input type=\"checkbox\" name=\"name\" value=\"". $dbreturnval ."\" ". $checked ." />\n"; Link to comment https://forums.phpfreaks.com/topic/43915-how-to-check-a-checkbox-based-on-database-value/#findComment-213230 Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 Ah yes, thanks for the correction emehrkay! Link to comment https://forums.phpfreaks.com/topic/43915-how-to-check-a-checkbox-based-on-database-value/#findComment-213255 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.