Jump to content


Photo

checkboxes in form


  • Please log in to reply
7 replies to this topic

#1 skullJ

skullJ
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 13 September 2006 - 02:47 PM

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)
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>
 	  }



And here it is my edit form's file:

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>
	}

Actually i wana use the iclude "form.php" when this input display thats why i use '$music_value' and '$music_check'.

Thank you! :)
...

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 13 September 2006 - 02:55 PM

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:
Enable music:<input type="checkbox" name="music" value="<?php echo $music_value ?>" checked="<?php echo $music_check ?>"><br>
to
echo 'Enable music:<input type="checkbox" name="music" value="'.$music_value.'"';
if ($music_check=="checked){
echo 'checked';
echo'><br>

Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 13 September 2006 - 03:18 PM

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:
<?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>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 06:19 AM

sorry... you're right. My mistake.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#5 skullJ

skullJ
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 15 September 2006 - 08:21 PM

hmmm...so the edit part of mine first post should be looks like:

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>
 	}

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!
...

#6 skullJ

skullJ
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 17 September 2006 - 04:20 PM

*bump*
...

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 17 September 2006 - 04:35 PM

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

<?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>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 skullJ

skullJ
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 19 September 2006 - 10:16 PM

sory i cant understand! :(

can someone write the right code? :-\
...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users