Page 1 of 1

Forms Question (Checkboxes)

Posted: Thu Nov 09, 2006 2:03 am
by 4Boredom
As I have my checkbox now.... It is like this.... I left it unchecked.

<input type="checkbox" id="q1" name="q1" VALUE="1">

This posts 1 if the user selects the box, 0 if nothing is selected.

I want to pull from the DB so that if 1 is in the DB the box opens up as checked, how is this possible

Posted: Thu Nov 09, 2006 2:35 am
by 4Boredom
I know this is wrong but it gets you started

Code: Select all

<input type="checkbox" id="q2" name="q2" VALUE="1" 
<?php
if ($q2 = 1){
	checked;
}
?>

Posted: Thu Nov 09, 2006 2:50 am
by jmut
4Boredom wrote:I know this is wrong but it gets you started

Code: Select all

<input type="checkbox" id="q2" name="q2" VALUE="1" 
<?php
if ($q2 = 1){
	checked;
}
?>
little typo. should be ==

Posted: Thu Nov 09, 2006 2:51 am
by Luke
Are you asking how to pull data from the database, or how to set the checkbox to checked?

Posted: Thu Nov 09, 2006 2:56 am
by 4Boredom
I know how to set it to checked..... users are setting their checkboxes to 1 if they want their questions to be shown and 0 if they dont.

Next time they load the edit profile page up I want the respective questions, or 1 variables, to be checked

Posted: Thu Nov 09, 2006 2:58 am
by Luke
OK... so you're asking how to get the data out of the mysql database?

Posted: Thu Nov 09, 2006 3:04 am
by 4Boredom
Im asking how to represent checked if they have the value 1 (for selected) entered into q1, q2, q3, or whatever respective question in the database

Posted: Thu Nov 09, 2006 3:17 am
by 4Boredom
This should help.... The default variable for checked is below

Code: Select all

<input type="checkbox" id="q2" name="q2" VALUE="1" checked>
However i need the following inside that input type to represent checked if q2 = 1... this is where help is needed


Code: Select all

<?php 
if ($q2 = 1){ 
   checked; 
} 
?>
The above code is wrong... but thats the best I can do

Posted: Thu Nov 09, 2006 5:53 am
by jmut
4Boredom wrote:This should help.... The default variable for checked is below

Code: Select all

<input type="checkbox" id="q2" name="q2" VALUE="1" checked>
However i need the following inside that input type to represent checked if q2 = 1... this is where help is needed


Code: Select all

<?php 
if ($q2 = 1){ 
   checked; 
} 
?>
The above code is wrong... but thats the best I can do
ok..really no idea what you're trying to do but... for second time

= should be == . as = is not comparison and it will always return true...hence marking all fields as checked..and having invalid html.

Code: Select all

$d = '<input type="checkbox" id="q2" name="q2" VALUE="1"';
if ($q2 == 1){ 
 $d .= ' checked ';
} 
 $d .= '>';
echo $d;