twhicher Posted June 28, 2006 Share Posted June 28, 2006 Hi guys, I'm brand new to PHP, and this type of coding. I've been working on a simple example from a tutorial where a form is used to update information on the same page;[i]<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="textfield"> <input type="submit" name="Submit" value="Submit"></form> <?PHPecho $_POST['textfield']; ?>[/i]However, i get the notice :" Undefined index: textfield in E:\web\northey\test6.php on line 14" when the form is first run - after a value has been submitted this goes, so it all works but i suffer from this unsightly error.I guess i need to somehow define $_post['textfield'] originally, but however i try to do this it either overrules the value comming in from the form, or appears to do nothing but duplicate the above error.Sorry for being a total No0b - I've searched and searched for a solution and am my wits end.PS the URL is [a href=\"http://www.northey.net/test6.php\" target=\"_blank\"]http://www.northey.net/test6.php[/a] and ive enabled phpinfo() to help out. Quote Link to comment https://forums.phpfreaks.com/topic/13135-undefined-index-issue/ Share on other sites More sharing options...
crazyone Posted June 28, 2006 Share Posted June 28, 2006 Hi, this is pretty simple and i'm actually going to give you a real push to form management in PHP.At the top of the PHP file you could write this: [code]<?phpif($_SERVER['REQUEST_METHOD'] == 'POST'){ if(isset($_POST['textfield']) && strlen($_POST['textfield']) > 0){ echo $_POST['textfield']; }else{ echo 'No textfield value submitted'; }}elseif($_SERVER['REQUEST_METHOD'] == 'GEST'){ $_POST['textfield'] = '';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13135-undefined-index-issue/#findComment-50509 Share on other sites More sharing options...
Orio Posted June 28, 2006 Share Posted June 28, 2006 The problem is, that if submit wasnt pressed (for example when the user enters in the first time to test6.php), the $_POST['textfield'] isnt set.So you need to check if submit was hit:[code]<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="text" name="textfield"><input type="submit" name="Submit" value="Submit"></form><?PHPif(isset($_POST['Submit'])){echo $_POST['textfield'];};?>[/code]Notice that I wrote $_POST['Submit'], when "Submit" is the [b]name[/b] of the submit button.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13135-undefined-index-issue/#findComment-50510 Share on other sites More sharing options...
twhicher Posted June 28, 2006 Author Share Posted June 28, 2006 Thankyou very much!! Quote Link to comment https://forums.phpfreaks.com/topic/13135-undefined-index-issue/#findComment-50522 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.