Jump to content

checkboxes in form


skullJ

Recommended Posts

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! :)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
<?php
if (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]
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

so how does he display the checkbox has been checked with a session then cheers.

[code]
<?php
if (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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.