skullJ Posted September 13, 2006 Share Posted September 13, 2006 hello,im trying to find some checkboxes help in form but i think my quetion is very specially!Here iam...this is my form's file that i wana see the checkbox as checked and my value when is checked as "on"(when is unchecked value will be "off").(in this "musictable" i have add the id as increasment)[code]if($submit) { $music= $_POST['music']; $result = mysql_query("INSERT INTO musictable (music) VALUES ('$music')",$connect);}else{ $music_value="on"; $music_check="checked"; Enable music:<input type="checkbox" name="music" value="<?php echo $music_value ?>" checked="<?php echo $music_check ?>"><br> } [/code] And here it is my edit form's file:[code]if($submit) { $music= $_POST['music'];$result = mysql_query("UPDATE musictable SET music='$music'WHERE id='$id' ",$connect);} elseif($id) { $result = mysql_query("SELECT * FROM musictable WHERE id='$id' ",$connect);while($myrow = mysql_fetch_assoc($result)) { if($myrow['music']=="on"){ $music_value="on"; $music_check="checked"; }else{ $music_value="off"; $music_check="0"; } Enable music:<input type="checkbox" name="music" value="<?php echo $music_value ?>" checked="<?php echo $music_check ?>"><br> }[/code]Actually i wana use the iclude "form.php" when this input display thats why i use '$music_value' and '$music_check'.Thank you! :) Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 13, 2006 Share Posted September 13, 2006 no no no... in your second file, you're saying checked = $music_check. what you need to do is end the echo statement first, then add the if satement to check whether the box is checked and if it is, then echo the word "checked".so change: [code]Enable music:<input type="checkbox" name="music" value="<?php echo $music_value ?>" checked="<?php echo $music_check ?>"><br>[/code]to[code]echo 'Enable music:<input type="checkbox" name="music" value="'.$music_value.'"';if ($music_check=="checked){echo 'checked';echo'><br>[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 13, 2006 Share Posted September 13, 2006 actually, checked does nothing in PHP. that's a javascript command. if you're wanting to see if a checkbox has been checked in a form submission, you have to check it with isset(). then, to keep the box checked, you've got to check if it is isset again:[code]<?phpif (isset($_POST['myCheck'])) { echo "You checked the box!\n";}?><form name="test" action="" method="post"><input type="checkbox" name="myCheck" value="1" <?php echo isset($_POST['myCheck']) ? 'checked="checked" ' : ''; ?>/> Check me!<br /><input type="submit" name="submit" value="Submit" /></form>[/code] Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 14, 2006 Share Posted September 14, 2006 sorry... you're right. My mistake. Quote Link to comment Share on other sites More sharing options...
skullJ Posted September 15, 2006 Author Share Posted September 15, 2006 hmmm...so the edit part of mine first post should be looks like:[code]if($submit) { $music= $_POST['music'];$result = mysql_query("UPDATE musictable SET music='$music'WHERE id='$id' ",$connect);} elseif($id) { $result = mysql_query("SELECT * FROM musictable WHERE id='$id' ",$connect);while($myrow = mysql_fetch_assoc($result)) { if(isset($_POST['nonhtml'])) $music = $myrow['music']; Enable music:<input type="checkbox" name="music" value="on" <?php isset($_POST['music'] ? 'checked="checked" ':' '?>/><br> }[/code]Am I right?And i have a second quetion...what kind of values can be in 'music'!I can use "on" and "off" or simply "1"?Thank u! Quote Link to comment Share on other sites More sharing options...
skullJ Posted September 17, 2006 Author Share Posted September 17, 2006 *bump* Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 17, 2006 Share Posted September 17, 2006 so how does he display the checkbox has been checked with a session then cheers. [code]<?phpif (isset($_POST['myCheck'])) { echo "You checked the box!\n";}?><form name="test" action="" method="post"><input type="checkbox" name="myCheck" value="1" <?php echo isset($_POST['myCheck']) ? 'checked="checked" ' : ''; ?>/> Check me!<br /><input type="submit" name="submit" value="Submit" /></form>[/code] Quote Link to comment Share on other sites More sharing options...
skullJ Posted September 19, 2006 Author Share Posted September 19, 2006 sory i cant understand! :(can someone write the right code? :-\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.