Multiple checkboxen

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

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


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.

It doesn’t work.:upset:

this is my code for my form

 <!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
//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.

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.

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

 
<!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
//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>
 

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:

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.

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

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’, ‘[email protected]’, ‘on’, ‘’);
The following data are submitted:
firstname: er
lastname: er
Adress: er 25
zip-code: 1562
city: bvgr
phone: 2+41+15654
Email: [email protected]
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.

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

dit is hoe je dat doet.. 

    $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>';
    

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.

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


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

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


<?
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
?>