Jump to content

Checkboxes not set value?


Mark82

Recommended Posts

Hi

 

I have a form with a series of 8 checkboxes.  The user can select as many checkboxes as they wish (i.e. none or all 8).  The values will be placed in a mysql databse.

 

My problem is, if the user doesnt check a checkbox, the value isnt sent via POST and the error....

 

ERROR: UNDEFINED INDEX

 

appears.

 

Is there a method to have a default value, ro check whether it hasn't been checked etc?

 

Any help appreicated, thanks.

 

Mark

Link to comment
Share on other sites

This example only has 3 checkboxes, but the principle is still the same:

 

<?php
require_once("dbFunctions.php");
dbConnect();

if(isset($_POST['Submit']))
{

$name = $_POST['txtname'];
$opt1 = $_POST['checkbox'];
$opt2 = $_POST['checkbox2'];
$opt3 = $_POST['checkbox3'];

		$query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('$name', '$opt1', '$opt2', '$opt3');"; 
		$result = dbQuery($query);

		echo "Results Added!";

}

?>

Link to comment
Share on other sites

<?php
require_once("dbFunctions.php");
dbConnect();

if(isset($_POST['Submit']))
{

if (isset($_POST['txtname']))
$name =  $_POST['txtname']  
else
$name = "";
if (isset($_POST['checkbox']))
$opt1 = $_POST['checkbox'];
else
$opt1 = "";
if (isset($_POST['checkbox2']))
$opt2 = $_POST['checkbox2'];
else
$opt2 = "";
if (isset($_POST['checkbox3']))
$opt3 = $_POST['checkbox3'];
else
$opt3 = "";

		$query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('$name', '$opt1', '$opt2', '$opt3');";
		$result = dbQuery($query);

		echo "Results Added!";

}

?>

Very bad way to do it, but it utilizes your current code.

Technically, you might aswell just do:

<?php
require_once("dbFunctions.php");
dbConnect();

if(isset($_POST['Submit']))
{

		$query="INSERT INTO alloyd (name, option1, option2, option3) VALUES ('".$_POST['txtname']."', '".$_POST['checkbox']."', '".$_POST['checkbox2']."', '".$_POST['checkbox3']."');";
		$result = dbQuery($query);

		echo "Results Added!";

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.