Jump to content

Undefined Index Issue.


twhicher

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.
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.