Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 131,631 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,217 people online right now. Registration is fast and FREE... Join Now!




Checkbox value

 
Reply to this topicStart new topic

Checkbox value

Arsench
post 10 Oct, 2008 - 05:42 AM
Post #1


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

Hello,
How can I set the checkbox value from db.
for example

CODE


here query select table_name

$1=$r["value_1"];
$2=$r["value2"];

<input type="checkbox" name="an" value="value_1"><?php echo"value1";?>


Ive tryed with value="<?php echo"value1";>" but didnt get the correct result

Thank you
User is offlineProfile CardPM

Go to the top of the page


CTphpnwb
post 10 Oct, 2008 - 06:26 AM
Post #2


D.I.C Regular

***
Joined: 8 Aug, 2008
Posts: 330



Thanked 19 times
My Contributions


This should make it clearer:
CODE
<?php

$value1="checked";
$value2='';
$value3="checked";

?>
<form method="post" action="<?php echo $php_self ?>"> <!-- Set up self post for submit button -->
<input type="checkbox" name="First" <?php echo $value1;?> >First check box<br>
<input type="checkbox" name="Second" <?php echo $value2;?> >Second check box<br>
<input type="checkbox" name="Third" <?php echo $value3;?> >Third check box<br>
<input type="submit" name="submit" value="Submit">
</form>

User is online!Profile CardPM

Go to the top of the page

Arsench
post 10 Oct, 2008 - 06:46 AM
Post #3


New D.I.C Head

*
Joined: 8 Oct, 2008
Posts: 4

QUOTE(CTphpnwb @ 10 Oct, 2008 - 07:26 AM) *

This should make it clearer:
CODE
<?php

$value1="checked";
$value2='';
$value3="checked";

?>
<form method="post" action="<?php echo $php_self ?>"> <!-- Set up self post for submit button -->
<input type="checkbox" name="First" <?php echo $value1;?> >First check box<br>
<input type="checkbox" name="Second" <?php echo $value2;?> >Second check box<br>
<input type="checkbox" name="Third" <?php echo $value3;?> >Third check box<br>
<input type="submit" name="submit" value="Submit">
</form>




thank you for you replay dear friend, but I dont need to check the chekbox after clicking, only i need to select checkbox value from db field.

thanks
User is offlineProfile CardPM

Go to the top of the page

CTphpnwb
post 10 Oct, 2008 - 07:17 AM
Post #4


D.I.C Regular

***
Joined: 8 Aug, 2008
Posts: 330



Thanked 19 times
My Contributions


Well, assuming that it is a mysql database, the query would look something like this:
CODE
$query = "SELECT * FROM thetablename where thecheckboxfield = 'on'";
$result=mysql($query);

Since $_POST['somecheckbox'] will be NULL if it's not checked, you'll probably want to set some other value in the database when storing:

CODE
$query = "Update thetablename set thecheckboxfield = 'off' where id= somevalue";
mysql($query);


EDIT: Of course, this depends on your table. You may not have a field named: id



This post has been edited by CTphpnwb: 10 Oct, 2008 - 07:18 AM
User is online!Profile CardPM

Go to the top of the page

Moonbat
post 10 Oct, 2008 - 12:02 PM
Post #5


D.I.C Regular

Group Icon
Joined: 30 Jun, 2008
Posts: 373



Thanked 22 times

Dream Kudos: 600
My Contributions


QUOTE(CTphpnwb @ 10 Oct, 2008 - 08:17 AM) *

Well, assuming that it is a mysql database, the query would look something like this:
CODE
$query = "SELECT * FROM thetablename where thecheckboxfield = 'on'";
$result=mysql($query);

Since $_POST['somecheckbox'] will be NULL if it's not checked, you'll probably want to set some other value in the database when storing:

CODE
$query = "Update thetablename set thecheckboxfield = 'off' where id= somevalue";
mysql($query);


EDIT: Of course, this depends on your table. You may not have a field named: id

I think you'll want to change this:

CODE
$result=mysql($query);


To this

CODE
$result=mysql_query($query);


It's a common mistake tongue.gif
User is online!Profile CardPM

Go to the top of the page

CTphpnwb
post 10 Oct, 2008 - 02:02 PM
Post #6


D.I.C Regular

***
Joined: 8 Aug, 2008
Posts: 330



Thanked 19 times
My Contributions


QUOTE(Moonbat @ 10 Oct, 2008 - 01:02 PM) *
It's a common mistake tongue.gif

That's what I get for copy/pasting instead of typing. wink2.gif
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 05:34AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month