Undefined Index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Undefined Index

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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'
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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>
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply