HELP!!! Problem with undefined index....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

HELP!!! Problem with undefined index....

Post by RossBryan »

Hey can anyone tell me why im getting the following undefined notice for the code straight after:
Notice: Undefined index: login in C:\xampp\htdocs\Care2Share\loginC2S.php on line 43

Code: Select all

<?php

$form = "<form action='index.php?login=yes' method='POST'>
 <table>
<tr>
	<td><input type='text' id='usernamebox' name='user' tabindex='1' value='Username' class='textbox' onfocus='usernamebox_focus();' onblur='usernamebox_blur();'></td>
</tr> 
<tr>
    <td><input type='text' id='passwordbox' name='pass' tabindex='2' value='Password' class='textbox' onfocus='passwordbox_focus();' onblur='passwordbox_blur();' /></td>
    <td><input type='submit' name='login' value='login' tabindex='3' class='button'> </td>
</tr>
</table>
</form>";

if ($_POST['login'])
{
		
}

else
{
	echo "$form";
}

?>
I'm quite sure im collecting the text name 'login' from inside the definition of the variable $form. But im still getting that notice in the browser for the part of the code where im attempting to apply $_POST['login'] in the if statement condition.

Can anyone help? I am new to this so is probably a pretty straight forward solution.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: HELP!!! Problem with undefined index....

Post by twinedev »

Well, how are you getting this message? Ware you browsing to loginC2S.php or are you browsing to index.php and it is including/requiring it?

Are you getting this message when you just browse to the page, or after you submit a form to the page in question (unless you have other checking to make sure the form is submitted, you WILL get that message the first time you go to the page if you are not arriving there from another form's submission

I always use:

Code: Select all

<?php
  if ($count($_POST}>0) {
    // Post action was carried out (ie. form submitted)
  }
  else {
    // First time call to page, so display form....
  }
?>
-Greg
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: HELP!!! Problem with undefined index....

Post by s.dot »

Use

Code: Select all

if (isset($_POST['login']))
If a variable may or may not be there, you can check if it's empty() or if it's isset()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

Re: HELP!!! Problem with undefined index....

Post by RossBryan »

Yes thanks i used the:

Code: Select all

if (isset ($_POST['login']))
approach and it now works fine, thank you. Would i do exactly the same thing if i wanted to assign variables to the other values in the form also; such as 'user' and 'pass', so as they can be verified?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: HELP!!! Problem with undefined index....

Post by s.dot »

Yes, since you don't know if the user will submit them or not. Using isset() though won't work because the value will be set but it will be empty.

Instead check if they're empty, such as..

Code: Select all

if (empty($_POST['user']) || empty($_POST['pass']))
{
    //show error
}
or

Code: Select all

if (!empty($_POST['user']) && !empty($_POST['pass']))
{
    //continue with processing form
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply