Skip to main content
April 27, 2010
Answered

Multiple checkboxes on a form

  • April 27, 2010
  • 1 reply
  • 1946 views

I'm trying to put a small group of checkboxes on a form here ---> www.hollisterairshow.com/helpusplan.php using spry validation

The form is for an airshow and we're asking site visitors which day are you planning to come - Saturday, Sunday or "Saturday and Sunday". They must select one day and may select both. I'm using MySQL to store the results and have set up a table with multiple columns, the ones for this question are called "Saturday" and "Sunday". If the site visitor selects Saturday, then the Saturday column should hold "Saturday" and if they select Sunday then the Sunday column should hold "Sunday". When a column is not selected the contents should be Null. When I test here's the results:

User selects"Saturday" column contents"Sunday" column contents
Saturday only"on""on"
Sunday only"Sunday" "Sunday"
Saturday and Sunday"Sunday" "Sunday"

            

The code is shown below

<p align="left">Which days are you visiting?

     <span id="sprycheckbox1">
        <label>
          <input type="checkbox" name="whichdays" id="whichdays_0" /> Saturday

               <span class="checkboxRequiredMsg">Please make a selection.</span>
         </label>

            <input name="whichdays" type="checkbox" value="Sunday"  id="whichdays_1"/> Sunday
     </span>

</p>  

I have no idea what's happening here and would really appreciate some help.

Thanks.

Tony                                             

This topic has been closed for replies.
Correct answer bregent

I think this is what you asked for, I'm not really sure as I'm new to this PHP

stuff.

<?php require_once('Connections/planconnection.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO plan (howArriving, howMany, Saturday, Sunday, camping, email, maillist, howHear) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['howarriving'], "text"),
                       GetSQLValueString($_POST['howMany'], "int"),
                       GetSQLValueString($_POST['whichdays'], "text"),
                       GetSQLValueString($_POST['whichdays'], "text"),
                       GetSQLValueString($_POST['RadioGroup1'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString(isset($_POST['maillist']) ? "true" : "", "defined","'Y'","'N'"),
                       GetSQLValueString($_POST['howDidYouHear'], "text"));

  mysql_select_db($database_planconnection, $planconnection);
  $Result1 = mysql_query($insertSQL, $planconnection) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hollister Airshow - Help Us Plan For You</title>
<link href="styles/thrColFixHdr.css" rel="stylesheet" type="text/css" />
<link href="styles/twoColFixLtHdr.css" rel="stylesheet" type="text/css" /><!--[if IE 5]>
<style type="text/css">
/* place css box model fixes for IE 5* in this conditional comment */
.twoColFixLtHdr #sidebar1 { width: 230px; }
</style>
<![endif]--><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.twoColFixLtHdr #sidebar1 { padding-top: 30px; }
.twoColFixLtHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->
<style type="text/css">
<!--
body {
background-image: url(images/sky2.jpg);
}
-->
</style>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationRadio.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationCheckbox.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationRadio.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationCheckbox.css" rel="stylesheet" type="text/css" />
</head>


You forgot to update the script to account for the new checkbox field names.

1 reply

Participating Frequently
April 27, 2010

You have given the check boxes the same names. Name one Saturday and the other Sunday.

April 27, 2010

Thanks. I tried renaming the boxes as shown in the code below and now I get "

Null for all tests

Here's the results I get

SelectedSaturdaySunday
SaturdayNULLNULL
SundayNULLNULL
Saturday and SundayNULLNULL

Here's the current code with the renamed boxes

<p align="left">Which days are you visiting?
     <span id="sprycheckbox1">
        <label>
          <input type="checkbox" name="saturday" id="whichdays_0" />
          Saturday<span class="checkboxRequiredMsg">Please make a selection.

                      </span>
         </label>

             <input name="sunday" type="checkbox" value="Sunday"  id="whichdays_1"/> Sunday

     </span>

</p>

Participating Frequently
April 28, 2010

Ok, I found the error, the html was missing the 'Value="Saturday"' statement. Thanks SO much for your help.

Tony


Cool. One more point...typically, you would make database columns a boolean datatype if they are associated with a form checkbox. The values would be either true or false.