# Multiple checkboxen

**URL:** <https://forum.kirupa.com/t/multiple-checkboxen/57618>\
**Category:** Uncategorized\
**Created:** [July 14, 2004, 6:55pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618 "2004-07-14T18:55:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mustang29](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@mustang29](https://forum.kirupa.com/u/mustang29)\
**Post date:** [July 14, 2004, 6:55pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/1 "2004-07-14T18:55:43Z")

</div>

Hey,

I’m working with mysql en php.

I have a form with checkboxen, but when I want to send them to the database, en get always an error. This error only comes when het checkbox isn’t checked. (error=undefined index).

Can someone help me to solve this problem?

Thx

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 7:18pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/2 "2004-07-14T19:18:09Z")

</div>

you have to set it to something, for example do something like this

```php

if(!$checkboxname) {
$checkboxname = 0;
} else {
$checkboxname = 1;
}

```

Then if it is checked, it will go into the database as 1 and if it is unchecked it will go into the database as 0.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 7:42pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/3 "2004-07-14T19:42:02Z")

</div>

It doesn’t work.:upset:

this is my code for my form

```php
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="test11.php" >
<h2>met checkboxen en knoppen</h2>
Voornaam: <input type="text" name="voornaam" size="40"><br/>
Achternaam: <input type="text" name="achternaam" size="40"><br/>
Adres en nr: <input type="text" name="adresennr" size="40"><br/>
Postcode: <input type="text" name="postcode" size="5"><br/>
Gemeente: <input type="text" name="gemeente" size="20"><br/>
Telefoon: <input type="text" name="telefoonnr" size="20"><br/>
email: <input type="text" name="email" size="40"><br/>
<hr>
<input name="veeteelt" type="checkbox" value="1">veeteelt<br>
<input name="koeien" type="checkbox" value="1">koeien<br>
<br>
<input name="Submit" type="submit" value="verzenden">
<input type="Reset" value="leegmaken"><br/>
</form>
</body>
</html>

```

this is my code for to insert into the database

```php

<?php
//verbinding met dbase
include("account.php");
 
if(empty($_POST))
{
echo("Vul eerst <a href\"test10.php\"> gegevens voor nieuwsbrief</a> in");
exit();
}
else
{
if(!$veeteelt) { 
$veeteelt = 0; 
} else { 
$veeteelt = 1; 
} 
if(!$koeien) { 
$koeien = 0; 
} else { 
$koeien = 1; 
} 
 
$query="INSERT INTO nieuwsb (voornaam, achternaam, adresennr, postcode, gemeente, telefoonnr, email, veeteelt, koeien) ";
$query .= "VALUES ('";
$query .= $_POST["voornaam"] ."', '" ;
$query .= $_POST["achternaam"] ."', '" ;
$query .= $_POST["adresennr"] ."', '" ;
$query .= $_POST["postcode"] ."', '" ;
$query .= $_POST["gemeente"] ."', '" ;
$query .= $_POST["telefoonnr"] ."', '" ;
$query .= $_POST["email"] ."', '" ; 
$query .= $_POST["veeteelt"] ."', '" ;
$query .= $_POST["koeien"] ."');" ;
$result = mysql_query($query) or die ("FOUT: " . mysql_error());
}
?>
<html>
<head>
<title>een werknemer toevoegen</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stijlen.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
echo("Query die wordt uitgevoerd: <b>$query</b><hr>
");
echo("De volgende gegevens zijn ingevoegd:<br>
");
echo("Voornaam: <b>". $_POST["voornaam"] . "</b><br>");
echo("Achternaam: <b>". $_POST["achternaam"] . "</b><br>");
echo("Adres en nr: <b>". $_POST["adresennr"] . "</b><br>");
echo("Postcode: <b>". $_POST["postcode"] . "</b><br>");
echo("Gemeente: <b>". $_POST["gemeente"] . "</b><br>");
echo("Telefoon: <b>". $_POST["telefoonnr"] . "</b><br>");
echo("Email: <b>". $_POST["email"] . "</b><br>");
echo("Veeteelt: <b>". $_POST["veeteelt"] . "</b><br>");
echo("Koeien: <b>". $_POST["koeien"] . "</b><br>");
/*echo("<hr><a href=\"frm_leverancier.php\">Nog een leverancier toevoegen</a> | 
<a href=\"leveranciertabel.php\">Overzicht van alle leveranciers</a>");*/
 
 
?>
</body>
</html>

```

the values of the checkboxen are recorded into the database.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 8:01pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/4 "2004-07-14T20:01:53Z")

</div>

sorry, that code gives me a headach to look at, i can’t get a good feeling of what you’re doing because I don’t understand the variable names hehe.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 8:14pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/5 "2004-07-14T20:14:36Z")

</div>

I hope you will understand the values names now. I have forgotten to translate them. :sleep:

```php
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="test11.php" >
 
firstname: <input type="text" name="firstname" size="40"><br/>
lastname: <input type="text" name="lastname" size="40"><br/>
adress: <input type="text" name="adress" size="40"><br/>
zip-code: <input type="text" name="zip-code" size="5"><br/>
city: <input type="text" name="city" size="20"><br/>
phone: <input type="text" name="phone" size="20"><br/>
email: <input type="text" name="email" size="40"><br/>
<hr>
<input name="newsletter" type="checkbox" value="1">newsletter<br>
<input name="questions" type="checkbox" value="1">questions<br>
<br>
<input name="Submit" type="submit" value="submit">
<input type="Reset" value="reset"><br/>
</form>
</body>
</html>
 

```

```php
 
<?php
//connect to dbase
include("account.php");
 
if(empty($_POST))
{
echo("Fill first <a href\"test10.php\">test</a> in");
exit();
}
else
{
if(!$newsletter) { 
$newsletter = 0; 
} else { 
$newsletter = 1; 
} 
if(!$questions) { 
$questions = 0; 
} else { 
$questions = 1; 
} 
 
$query="INSERT INTO nieuwsb (firstname, lastname, adress, zip-code, city, phone, email, newsletter, questions) ";
$query .= "VALUES ('";
$query .= $_POST["firstname"] ."', '" ;
$query .= $_POST["lastname"] ."', '" ;
$query .= $_POST["adress"] ."', '" ;
$query .= $_POST["zip-code"] ."', '" ;
$query .= $_POST["city"] ."', '" ;
$query .= $_POST["phone"] ."', '" ;
$query .= $_POST["email"] ."', '" ; 
$query .= $_POST["newsletter"] ."', '" ;
$query .= $_POST["questions"] ."');" ;
$result = mysql_query($query) or die ("FOUT: " . mysql_error());
}
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stijlen.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
echo("Query who will be done: <b>$query</b><hr>
");
echo("The following data are submitted:<br>
");
echo("firstname: <b>". $_POST["firstname"] . "</b><br>");
echo("lastname: <b>". $_POST["lastname"] . "</b><br>");
echo("Adress: <b>". $_POST["adress"] . "</b><br>");
echo("zip-code: <b>". $_POST["zip-code"] . "</b><br>");
echo("city: <b>". $_POST["city"] . "</b><br>");
echo("phone: <b>". $_POST["phone"] . "</b><br>");
echo("Email: <b>". $_POST["email"] . "</b><br>");
echo("newsletter: <b>". $_POST["newsletter"] . "</b><br>");
echo("questions: <b>". $_POST["questions"] . "</b><br>");
 
 
 
?>
</body>
</html>
 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 8:21pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/6 "2004-07-14T20:21:51Z")

</div>

the problem is that you have value=1 set that means that no matter whether they are checked or not they will come out true, if you want them to be checked by default use \<input type=“checkbox” name=“blah” checked\>

I’m not sure if there’s anything else wrong but that’s what I saw right off the bat.

:thumb:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 8:33pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/7 "2004-07-14T20:33:54Z")

</div>

the checkboxen mustn’t be checked. But even if I set no value near to the checkboxen, it gives always an error. The error must be (to my mind) that there is somthing wrong with the second code. The code that put the values into the databse. He says

> 

Notice: Undefined variable: newsletter in C:\www est newsletter est11.php on line 12  
Notice: Undefined variable: questions in C:\www est newsletter est11.php on line 18

Notice: Undefined index: newsletter in C:\www est newsletter est11.php on line 34

Notice: Undefined index: questions in C:\www est newsletter est11.php on line 35

The name of the checkboxen are recorded in the database.  
When I check a checkbox, the problem is solved, but when I don’t check a checkbox, he gave the error above.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 8:42pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/8 "2004-07-14T20:42:23Z")

</div>

comment out all of the $query and mysql\_query, and tell me what your code prints out from the echos.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 14, 2004, 9:07pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/9 "2004-07-14T21:07:35Z")

</div>

It’s like he can’t find het name of the checkboxen.

this is what I get from error when I don’t check a checkbox

> 

**Notice** : Undefined variable: newsletter in **C:\www est newsletter est11.php** on line **12**

**Notice** : Undefined variable: questions in **C:\www est newsletter est11.php** on line **18**

**Notice** : Undefined index: newsletter in **C:\www est newsletter est11.php** on line **34**

**Notice** : Undefined index: questions in **C:\www est newsletter est11.php** on line **35**

Query who will be done: **INSERT INTO nieuwsb2 (firstname, lastname, adress, zipcode, city, phone, email,newsletter, questions) VALUES (‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’);**  
The following data are submitted:  
firstname:  
lastname:  
Adress:  
zip-code:  
city:  
phone:  
Email:

**Notice** : Undefined index: koeien in **C:\www est newsletter est11.php** on line **59**  
newsletter:

**Notice** : Undefined index: veeteelt in **C:\www est questions est11.php** on line **60**  
questions:

when I check a checkbox, I get the following

> 

**Notice** : Undefined variable: newsletter in **C:\www est newsletter est11.php** on line **12**

**Notice** : Undefined variable: questions in **C:\www est newsletter est11.php** on line **18**

**Notice** : Undefined index: questions in **C:\www est newsletter est11.php** on line **35**  
Query who will be done: **INSERT INTO nieuwsb2 (firstname, lastname, adress, zipcode, city, phone, email, newsletter, questions) VALUES (‘er’, ‘er’, ‘er 25’, ‘1562’, ‘bvgr’, ‘2+41+15654’, ‘rfgd@hotmail.com’, ‘on’, ‘’);**  
The following data are submitted:  
firstname: **er**  
lastname: **er**  
Adress: **er 25**  
zip-code: **1562**  
city: **bvgr**  
phone: **2+41+15654**  
Email: **[rfgd@hotmail.com](mailto:rfgd@hotmail.com)**  
newsletter: **on**

**Notice** : Undefined index: questions in **C:\www est newsletter est11.php** on line **60**  
questions:

when I check my database, he says always that the checkbox isn’t checked even it is checked.  
The problem is that I don’t know where I have to declare the values of the name from the checkbox that the also knows it in the database.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 15, 2004, 3:34am UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/10 "2004-07-15T03:34:51Z")

</div>

i am guessing you want to check if all fields are set… correct?

```
dit is hoe je dat doet.. 

```

```php

    $koeien='';
    $veeteelt='';
   #checking if button submit has been pressed.
    if (isset($_POST['submit'])) 
    {
   # checking if koeien field has value.
    if (strlen($_REQUEST['koeien'])>0) 
    {
    $koeien='1';
    }
    else{$koeien='Koeien must be set';
    }
   # checking if veeteelt field has value.
    if (strlen($_REQUEST['veeteelt'])>0) 
    {
    $veeteelt='1';
    }
    else{$veeteelt='veeteelt must be set';
    }
   # if all required fields have been set do query
    if ($koeien=='1' && $veeleelt=='1') 
    {
    $query="INSERT INTO nieuwsb (koeien, veeteelt) VALUES ("'._$REQUEST['kooien'].'","'.$_REQUEST['veeteelt'].'")";
    $result=mysql_query($query) or die (mysql_error());
    echo 'succesvol ';
   #exit to not make the form show itself again.
    exit();
    }
    }
   #start of page. or when required fields are empty .
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    echo '<br><table border="0" cellpadding="0" cellspacing="0">';
    echo '<tr>';
    if($koeien == '1')
    {
 echo '<td width="25%" align="left"><b>Koeien</td><td><INPUT TYPE="text" NAME="koeien" value="'.$_POST['koeien'].'"></td>';
    }else{
 echo '<td width="25%" align="left"><b>Koeien</td><td><INPUT TYPE="text" NAME="koeien" value="">'.$koeien.'</td>';
    }
    echo '</tr><tr>';
    if($veeteelt == '1')
    {
 echo '<td width="25%" align="left"><b>Veeteelt</td><td><INPUT TYPE="text" NAME="veeteelt" value="'.$_POST['veeteelt'].'"></td>';
    }else{
 echo '<td width="25%" align="left"><b>Veeteelt</td><td><INPUT TYPE="text" NAME="veeteelt" value="">'.$veeteelt.'</td>';
    }
    echo '</tr></table>';
    echo '<center><br><INPUT TYPE="submit" name="submit" value="submit"></form>';
    

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 15, 2004, 12:49pm UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/11 "2004-07-15T12:49:15Z")

</div>

The fact is that it can be that there is a checkbox that can’t be checked. It isn’t necessary checked. Both checkboxen can be checked, but there can also be one checkbox checked.  
The code you gave above controls of bhoth checkboxen are checked. When one isn’t checked, he gave an error.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 16, 2004, 5:07am UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/12 "2004-07-16T05:07:51Z")

</div>

for checkboxes its the same (kinda), all my post are untested, and written on the spot… so errors can occure ofcourse …

```php

 echo '<Input type="checkbox" name="koeien" value="1">Koeien';
 # testing it..
 if (!empty($_POST['koeien']))
 { echo 'koeien'; }
 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 16, 2004, 5:27am UTC](https://forum.kirupa.com/t/multiple-checkboxen/57618/13 "2004-07-16T05:27:07Z")

</div>

This is a working example of is the checkbox checked yes orno.

```php

<?
echo '<form action='.$_SERVER['PHP_SELF'].' method="post">';
echo '<INPUT TYPE="checkbox" NAME="koeien">Koeien';
echo '<INPUT TYPE="checkbox" NAME="veeteelt">Veeteelt';
echo '<INPUT TYPE="submit" name="submit">';

if (isset($_POST['submit']))
{
if (empty($_POST['koeien']))
{ echo '<br>koeien niet checked<br>';
}

if (empty($_POST['veeteelt']))
{ echo '<br>veeteelt niet checked<br>';
}

}#end if submit
?>

```
