Some Help with my Unidentified Index error

Hi I am a still a beginner in programming and I wanted to know if you guys could help me on what is cause the error to pop out. If you guys could track what is wrong on my codes could you also try to correct it? Thanks in advanced.

<?php
$username= $_POST['username'];

$password= $_POST['password'];

if ($username== "j12345" and $password== "spcm")
{
echo "<img src=right.jpg>";
}
else
{
echo "<img src=wrong.jpg>";
}
?>	

Hi Josel Amiel, welcome to the forums

The code is wanting too assign the values of the $_POST array members having “username” and “password” as indexes to variables.
The $_POST array at that point in the code does not contain one or both.

You will get that undefined index error when the POST key or any array key is not present. It’s a good idea to wrap any code dealing with these keys in a isset() statement so it is only used when it IS SET.

<?php
if(isset($_POST['username'])):
	$username= $_POST['username'];
	
	$password= $_POST['password'];
	
	if ($username== "j12345" and $password== "spcm")
	{
	echo "<img src=right.jpg>";
	}
	else
	{
	echo "<img src=wrong.jpg>";
	}
endif;
?>

At Drummin
I tried the code above and it did work, just one problem for me now after this : It leads me to nothing. I made a HTML before which was the login page for my PHP but when i login with the right info it just leads me to a white space. Any ideas on why this happened?

A white screen usually means a fatal error occurred. This probably happens while making the query. If you have access to the errors logs it would tell you what is causing this error. Did your host upgrade to a new version of php? If you can post your full login code maybe someone can spot the issue.

There are quotes missing which could be causing the blank page from appearing.

Try this:


    // MISSING QUOTES
    // echo "<img src=right.jpg>";  

   // ADDED QUOTES
   echo '<img src="right.jpg"  alt="right"  />'; 
    // OR THIS
   echo "<img src='right.jpg'  alt='right'  /> "; 
; 

    // also try setting defaults to your $_POST variables.
   $username= isset( $_POST['username'] )  ? $_POST['username'] : FALSE;
   $password= isset( $_POST['password']  ) ? $_POST['password'] : FALSE ;


Yes I didn’t “fix” any code within what was posted. I only was pointing out that the code should not be run if POST isn’t present as you I assume you are querying the DB for username and password for the login script. Wrap it within an IF condition. See if you can isolate the problem area by commenting parts out. Here’s adjusted code.

<?php
if(isset($_POST['username'])):
    $username = $_POST['username'];

    $password = $_POST['password'];

    if ($username == "j12345" && $password == "spcm")
    {
    echo '<img src="right.jpg">';
    }
    else
    {
    echo '<img src="wrong.jpg">';
    }
	//The rest of your login code
	/*
	You can also comment out sections of your code
	to isolate the blank screen error like I've done
	with this text. More than likely when viewing the
	source of the blank screan you will see a 1.
	Not much help, I know. As you seem to be just
	using these images to test POST then isolate the
	problem then ask for help with the problem code or
	post the whole page you are having trouble with.
	*/
endif;
?>

Whenever I have blank screens I use the following script to display the line number and stop the remaining page from displaying.

If the line number displays OK then I move the line to halfway down the page. If the page is blank then I move the line halfway up the previous lines. This procedure is repeated until the offending script with the error is located.


<?php 
echo '<br />line: ' .__LINE__;die; 
// moved either up or down depending whether or not line number shows.
...
...
...



<?php

if (isset($_POST['username'],$_POST['password'])) {
    ## Proceed your form check
    var_dump($_POST);
}

?>

<form method="POST" action="">
    <input type="text" name="username"><br>
    <input type="password" name="password"><br>
    <input type="submit" value="login">
</form>

Hi Cornex and welcome to the site.

I did not know that isset() accepts more than one parameter, many thanks for your valuable input.

http://www.php.net/manual/en/function.isset.php

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

I didn’t know that either. Cool.