I forgot to set my name attributes in a form today (deeeeer!), and was scratching my head a bit when this error started coming up for all my form elements:
"PHP Notice: Undefined index:"
This is an easy enough problem to fix. Depending on your DTD, adding name attibutes in addition to id attributes will make the form post the data correctly. Here is a bit of a test you can try for yourself.
<form action="send.php" method="POST" id="contactForm" enctype="multipart/form-data">
<fieldset>
<label for="Name">Your Name </label>
<input id="Name" type="text" />
<input id="sendMail" type="submit" name="submit" />
</fieldset>
</form>
Here is your basic form. In the page 'post.php' we add the following:
<?php var_dump($_POST); ?>
If we then posted our form, we would get the "PHP Notice: Undefined index:" error in our debugger, and you would get the following array from var_dump
array(1) { ["submit"]=> string(12) "Submit Query" }
Notice that the array has one key "submit". Some forums suggest using if(isset($_POST['submit'])) {} to solve this error, but obviously since this key exists this is not necessarily the best solution. Another solution would be to experiment with the form to see which attributes cause the input values to show up in the $_POST array. Modify your "Name" input element to include:
name="Name"
Post the form again and we get...
array(2) { ["Name"]=> string(5) "Aaron" ["submit"]=> string(12) "Submit Query" }
Ah! That's better. This time I can see my "Name" input value has been set to Aaron and successfully posted, which is what I want to happen.
This may seem like an overkill solution to this problem, but consider that var_dump may be useful to debug forms with a lot of post values; as an alternative to writing a whole heap of if statements to test which values are set in form post. Once we see that $_POST is an array/array like object (?) we can use the regular array syntax to get the post value, e.g. $_POST['Name']
I hope that made someone's day a little less frustrating ;)
P.S. Jayne said she'd bring me a cookie if I posted this duck -enjoy!
,----,
___.` `,
`=== D :
`'. .'
) ( ,
/ \_____________/|
/ |
| ;
| _____ /
| \ ______7 ,'
| \ ______7 /
\ `-,____7 ,' jgs
^~^~^~^`\ /~^~^~^~^
~^~^~^ `----------------' ~^~^~^
~^~^~^~^~^^~^~^~^~^~^~^~^~^~^~^~
Comments [0]