Page 1 of 1

Undefined Index

Posted: Sat Jan 25, 2003 9:18 am
by Zeceer
I have a form where there are 3 checkboxes. When the form is sent to the script i get this error for the boxes that are not checked:
Notice: Undefined index: logodesign in D:\bestill.php on line 24

Notice: Undefined index: visittkort in D:\bestill.php on line 25
This is what the script looks like:
$to = "zeceer@hotmail.com";
$subject = "Order";
$headers = "zeceer@hotmail.com";

$body =
"Kontakt Informasjon"."\n\n".

"Navn: ".$_POST['navn']."\n".
"Adresse: ".$_POST['adresse']."\n".
"Postnummer: ".$_POST['postnummer']."\n".
"Sted: ".$_POST['sted']."\n".
"Telefonnummer: ".$_POST['telefonnummer']."\n".
"E-mail: ".$_POST['email']."\n\n".

"Tjeneste Valg"."\n\n".

"Logodesign:".$_POST['logodesign']."\n".
"Visittkort:".$_POST['visittkort']."\n".
"Web Banner:".$_POST['webbanner']."\n\n".

"Kommentar"."\n\n".

$_POST['kommentar'];

mail($to,$subject,$body,"From: $headers");
Anyone?

Posted: Sat Jan 25, 2003 9:44 am
by volka
that's because unchecked checkboxes are not posted at all.
If checked they are transmitted as name=On :arrow: $_POST['name']='On'
You might test this e.g. with isset($_POST['name']) && $_POST['name']=='On'

Posted: Sat Jan 25, 2003 10:13 am
by hedge
You also need to be careful if you are building an array of them. When you build the html use checkbox[0], checkbox[1]... instead of checkbox[], checkbox[]. If you don't do this then the rows in your array will not match up because PHP only builds the array with the 'on' values.

Posted: Sat Jan 25, 2003 10:17 am
by volka
addition: of course 'on' is only the default value ;)

Code: Select all

<html><body>
<pre><?php print_r($_POST); ?></pre>
<form method="post">
	<input type="checkbox" name="chk[]" value="a" /><br />
	<input type="checkbox" name="chk[]" value="b" /><br />
	<input type="checkbox" name="chk[]" value="c" /><br />
	<input type="checkbox" name="chk[]" value="d" /><br />
	<input type="submit" />
</form>
</body></html>

Posted: Sat Jan 25, 2003 1:04 pm
by Zeceer
ok, so the problem can be solved by testing if the variable exist with isset()? Then I can make the script print the variable if it exist, and not print it if doesn't?

By the way, is there any connection with checkboxes and global variables? On my local webserver I have global turned off. But on my webhotel they are turned on. On the webhotell this error doesn't accour at all.

Posted: Sat Jan 25, 2003 2:48 pm
by twigletmac
The reason why the error message doesn't appear when you run your script on your host's server is not to do with register_globals but to do with the level of error reporting being used.

For more info on error reporting:
http://www.php.net/manual/en/function.e ... orting.php

Mac