Jump to content

Archived

This topic is now archived and is closed to further replies.

twhicher

Undefined Index Issue.

Recommended Posts

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>

<?PHP
echo $_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.

Share this post


Link to post
Share on other sites
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]<?php
if($_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]

Share this post


Link to post
Share on other sites
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>

<?PHP
if(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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.