Page 1 of 1
I'm confused about how to use checkboxes..
Posted: Tue May 27, 2003 10:58 am
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
Posted: Tue May 27, 2003 11:06 am
by werlop
Hi, could you post your code please

Posted: Tue May 27, 2003 11:01 pm
by scorphus
Before doing database operations check to see if the variable is set using
isset().
Regards,
Scorphus.
Posted: Wed May 28, 2003 2:29 am
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)
{
echo"<P>Couldn't add record!";
}
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 {
FONT-WEIGHT: bold; FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.5in; FONT-FAMILY: "Times New Roman"
}
</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> <INPUT type=radio
value=Intermediate name=ITSKILLS><SPAN
style="FONT-WEIGHT: 400">Intermediate</SPAN> <INPUT type=radio
value=Advanced name=ITSKILLS> <SPAN
style="FONT-WEIGHT: 400">Advanced</SPAN> <INPUT type=radio value=Expert
name=ITSKILLS> <SPAN style="FONT-WEIGHT: 400">Expert</SPAN> <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"> <BR>2. (A)
Have you used or would you like to use the following in the future to
assist in your course instruction?<BR> </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
</SPAN><INPUT type=checkbox value=Yes
name=Q2AMATS_USED>
<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
</SPAN><INPUT type=checkbox value=Yes
name=Q2BMATS_USED>
<INPUT type=checkbox value=Future name=Q2BMATS_FUTURE></P>
<P>
</P>
<P> </P>
<P> </P><INPUT type=submit value=Submit name=control>
<P align=left> </P></FORM></BODY></HTML>
Posted: Wed May 28, 2003 2:38 am
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";
}
?>