1

I looked trough your pages and fixed my undefined index on username and password but now I get undefined variable. I had more code but decided to simplify it for the sake of testing and just echo it to see if anything changes and it doesn't.

<?php
if (isset($_POST['submit'])){
$uname = isset($_POST['username']);             
$pass = isset($_POST['password']); 
}

echo $uname;
echo $password;
?>

I get this:

Notice: Undefined variable: uname in C:\xampp\htdocs\8\login.php on line 7

Notice: Undefined variable: pass in C:\xampp\htdocs\8\login.php on line 8

I do not really understand what's wrong here. If needed here is my form page html.

<form id="login" method="post" action="login.php">
<div id="loginon">
<label for="username" style="color:#0F0; font-family:Arial, Helvetica, sans-serif;" >Username: &nbsp; &nbsp;</label>
<input type="text" id="username" name="username" />
<br  />
<br  />

<label for="password"  style="color:#0F0; size:auto; font-family:Arial, Helvetica, sans-serif;">Password: &nbsp; &nbsp;</label>
<input type="password" id="password" name="password" /> 
<br />
<br />
<input type="image" src="http://www.clker.com/cliparts/2/G/4/v/K/E/submit-hi.png" border="0" width="180px" height="80px" alt="Submit" />
</div>
</form>
</body>

EDIT: The further problem is connected to this so I think I am allowed to post it here.

if($username == "pikachu" || "cloud") {
    $ucheck = "dobar";
} else {
    $ucheck = "los";
}

if ($password == "123123" || "132132") {
    $pcheck = "topar";
} else {
    $pcheck = "losh";
}

if($ucheck == "dobar" && $pcheck == "topar") {
    echo "Najjaci si!";

}

elseif ($ucheck == "dobar" && $pcheck == "losh") {
    echo "Wimp.";
} elseif ($ucheck == "los" && $pcheck == "topar") {
    echo "Fish.";
}

The problem is that it always echoes out "Najjaci si" no matter what I type in the previous form. Any ideas?

victiMusPrime
  • 53
  • 2
  • 9

3 Answers3

2

Since you have the variable decleration inside a logic (If), there is a chance that the variables are not set at all. Try this

<?php
if (isset($_POST['submit'])){
  $uname = isset($_POST['username']);             
  $pass = isset($_POST['password']); 

  echo $uname;
  echo $pass;
} else {
  echo "No submit detected!";
}


?>

It also doesen't look like if you have an actual element named submit. Try adding this:

<input type="submit" name="submit" value="Send my form" />

Now, your form contains an element named "submit" which will be send as a post parameter when the form is submitted.

I myself, in need of a framework, like doing like this:

$username = ( isset($_POST["username"]) && $_POST["username"] != "" ? $_POST["username"] : false );
$password = ( isset($_POST["password"]) && $_POST["password"] != "" ? $_POST["password"] : false );

if( !$username === false && !$password === false )
{
    echo $username . " <br /> " . $password;
} else {
    echo "No! :) ";
}
Jonas m
  • 2,646
  • 3
  • 22
  • 43
1

isset will check that variable is set or not and pass value in 0 or 1. So over here you need to do as

$uname = isset($_POST['username']) ? $_POST['username'] : '';             
$pass = isset($_POST['password']) ? $_POST['password'] : ''; 

Notice: Undefined variable: pass in C:\xampp\htdocs\8\login.php on line 8

Its because you were echoing $password instead of $pass

Edited

Do it as

$uname = '';
$pass = '';

if (isset($_POST['submit'])){
$uname = isset($_POST['username']);             
$pass = isset($_POST['password']); 
}

echo $uname;
echo $pass;
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
0

Try this

In form

<form id="login" method="post" action="login.php">
    <div id="loginon">
        <label for="username" style="color:#0F0; font-family:Arial, Helvetica, sans-serif;" >Username: &nbsp; &nbsp;</label>
        <input type="text" id="username" name="username" />
        <br  />
        <br  />

        <label for="password"  style="color:#0F0; size:auto; font-family:Arial, Helvetica, sans-serif;">Password: &nbsp; &nbsp;</label>
        <input type="password" id="password" name="password" />
        <br />
        <br />
        <input type="submit" name="submit"/>
    </div>
</form>
</body>

In login.php

<?php
    if (isset($_POST['submit']))
    {
        $uname = $_POST['username'];
        $pass = $_POST['password'];
    }

    echo $uname;
    echo $pass;
?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85