Forms Question (Checkboxes)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Forms Question (Checkboxes)

Post 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
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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;
}
?>
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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 ==
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Are you asking how to pull data from the database, or how to set the checkbox to checked?
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK... so you're asking how to get the data out of the mysql database?
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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;
Post Reply