Jump to content


Photo

Undefined Index Issue.


  • Please log in to reply
3 replies to this topic

#1 twhicher

twhicher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 June 2006 - 06:19 PM

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;

<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'];
?>

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.

#2 crazyone

crazyone
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 June 2006 - 06:37 PM

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:

<?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'] = '';
}
?>


#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 28 June 2006 - 06:40 PM

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:

<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'];
};
?>

Notice that I wrote $_POST['Submit'], when "Submit" is the name of the submit button.


Orio.
Think you're smarty?

(Gone until 20 to November)

#4 twhicher

twhicher
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 June 2006 - 07:11 PM

Thankyou very much!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users