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
https://forums.phpfreaks.com/topic/113326-checkboxes-not-set-value/
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!";

}

?>

<?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!";

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.