I'm confused about how to use 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
sbhatt
Forum Newbie
Posts: 1
Joined: Tue May 27, 2003 10:58 am
Location: project manager

I'm confused about how to use checkboxes..

Post by sbhatt »

Can anyone please help me?

I've an html form with checkboxes. I want the users to click only those check boxes which are applicable to them. I want to store their answers in my database.

When I click on all the checkboxes on the form, the script runs successfully but when I leave one of the checkboxes unmarked, I get the following error:

Undefined variable: Q2AMATS_FUTURE in C:\Inetpub\wwwroot\phpproject\checkbox.php on line 10

though everything gets recorded in my database..

thanks
sbhatt
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Hi, could you post your code please :D
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Before doing database operations check to see if the variable is set using isset().

Regards,
Scorphus.
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Right this is the code that sbhatt sent to me:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> 
<HTML> 
<!--METADATA TYPE="type.lib" FILE="C:\Program Files\Common Files\System\ado\msado15.dll"--> 


<BODY> 

<?php 

$sql="INSERT INTO access(ITSkills, 2AMats_Used, 2AMats_Future, 2BMats_Used, 2BMats_Future) VALUES ('$ITSKILLS', '$Q2AMATS_USED', '$Q2AMATS_FUTURE', '$Q2BMATS_USED', '$Q2BMATS_FUTURE')"; 

$connection = mysql_connect("localhost") or die("Couldn't connect to server."); 

$db = mysql_select_db("test",$connection) or die("Couldn't select database."); 

$sql_result=mysql_query($sql,$connection) or die("Couldn't execute query."); 

if(!$sql_result) 

&#123; 
echo"<P>Couldn't add record!"; 
&#125; 


mysql_close($connection); 


?> 

and my html code is as follows: 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<!-- saved from url=(0101)file://C:\Documents%20and%20Settings\FDU\Local%20Settings\Temporary%20Internet%20Files\OLKA\page1.htm --><HTML><HEAD><TITLE>1</TITLE> 
<META http-equiv=Content-Language content=en-us> 
<META content="MSHTML 6.00.2800.1106" name=GENERATOR> 
<META content=FrontPage.Editor.Document name=ProgId> 
<META http-equiv=Content-Type content="text/html; charset=windows-1252"> 
<STYLE>P.MsoBodyTextIndent &#123; 
FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.5in; FONT-FAMILY: "Times New Roman" 
&#125; 
</STYLE> 
</HEAD> 
<BODY> 
<FORM name=signup action=checkbox.php method=post> 
<P class=MsoBodyTextIndent style="MARGIN-LEFT: 0in; TEXT-INDENT: 0in"><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">1. How would you rate your 
information technology skills?</SPAN></P> 
<P><INPUT type=radio value=Beginner name=ITSKILLS><SPAN 
style="FONT-WEIGHT: 400">Beginner </SPAN>&nbsp;<INPUT type=radio 
value=Intermediate name=ITSKILLS><SPAN 
style="FONT-WEIGHT: 400">Intermediate</SPAN>&nbsp; <INPUT type=radio 
value=Advanced name=ITSKILLS> <SPAN 
style="FONT-WEIGHT: 400">Advanced</SPAN>&nbsp; <INPUT type=radio value=Expert 
name=ITSKILLS> <SPAN style="FONT-WEIGHT: 400">Expert</SPAN>&nbsp; <INPUT 
type=radio value=Guru name=ITSKILLS> <SPAN 
style="FONT-WEIGHT: 400">Guru</SPAN></P> 
<TABLE id=AutoNumber1 style="BORDER-COLLAPSE: collapse" borderColor=#111111 
cellSpacing=0 cellPadding=0 width="79%" border=1> 
<TBODY> 
<TR> 
<TD width="56%" bgColor=#ffffcc> 
<P align=left><B><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">&nbsp;&nbsp; <BR>2.&nbsp;(A) 
Have you used or would you like to use the following in the future to 
assist in your course instruction?<BR>&nbsp;</SPAN></B></P></TD> 
<TD width="15%" bgColor=#ffffcc> 
<P align=center><SPAN style="FONT-WEIGHT: 700; FONT-FAMILY: Arial"><FONT 
size=2>Have Used</FONT></SPAN></P></TD> 
<TD width="29%" bgColor=#ffffcc> 
<P align=center><SPAN 
style="FONT-WEIGHT: 700; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Would like 
to use it in the future</SPAN></P></TD></TR></TBODY></TABLE> 
<P align=left><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">a) PowerPoint 
handouts to accompany 
lectures&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</SPAN><INPUT type=checkbox value=Yes 
name=Q2AMATS_USED>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<INPUT type=checkbox value=Yes name=Q2AMATS_FUTURE></P> 
<P><FONT size=2><SPAN style="FONT-FAMILY: Arial">(b) </SPAN></FONT><SPAN 
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">PowerPoint Presentations using a 
computer 
projector&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
</SPAN><INPUT type=checkbox value=Yes 
name=Q2BMATS_USED>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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2003 2:50 am
Location: /dev/null

Post by werlop »

I have not looked at the code fully but something like:

Code: Select all

<?php

if ( ! isset ( $_POST["name_of_input"] ) ) {
   $var_from_input = "No";
   } else {
   $var_from_input = "Yes";
   }

?>
may work.

or you could do:

Code: Select all

<?php

if ( empty ( $_POST["name_of_input"] ) ) {
   $var_from_input = "No";
   } else {
   $var_from_input = "Yes";
   }

?>
Post Reply