Page 1 of 1

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

Posted: Tue Aug 23, 2011 8:13 pm
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.

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

Posted: Tue Aug 23, 2011 10:09 pm
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

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

Posted: Wed Aug 24, 2011 10:25 am
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()

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

Posted: Wed Aug 24, 2011 10:35 am
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?

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

Posted: Wed Aug 24, 2011 11:08 am
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
}