Jump to content


Photo

how to echo a checkbox and radio button (if checked or not)


  • Please log in to reply
3 replies to this topic

#1 Deserteye

Deserteye
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationNew Jersey

Posted 18 October 2006 - 12:14 AM

I have a script where a user has options to choose if he/she wants to recieve emails from other members. but I want the user to be able to edit that. When they go into their user cp, how would I echo either if the checkbox is checked or not by what it is currently set to?

What I know:
XHTML: 10/10
CSS: 8/10
JavaScript: 5/10
VBScript: 7/10 PHP: 8/10 MySQL: 9/10

#2 chriscloyd

chriscloyd
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts
  • LocationArizona

Posted 18 October 2006 - 05:45 AM

well did u save them into a database when they choose if they want to or not?
44 bugs in my java code
44 bugs in my java code
Fix 1 bug, and complie again
122 bugs in my java code

#3 manichean

manichean
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 18 October 2006 - 05:52 AM

All you have to do is get the value (presumably you storing it in a databse) and perform the following

For a checkbox (you do the same for a radio button)

<?php
// This assumes you have pulled the info from a database storing it into $row['checkbox']
// and that you know the value to either be true or false
if ($row['checkbox']){
$checkboxChecked = "checked=\"checked\"";
}else{
$checkboxChecked = "";
}
?>
<input type="checkbox" name="checkbox" value="checkbox" id="checkbox" <?php echo $checkboxChecked ?> />




#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 18 October 2006 - 07:28 AM

Shorter:
<input type="checkbox" name="checkbox" value="checkbox" id="checkbox"<?php echo $row['checkbox'] ? ' checked="checked"' : null; ?> />





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users