Am I loosing the plot or is something strange happening here?

I have a simple login form (see below), but when I call the page, PHP returns and undefined index error, even though I have coded to say if index is undefined, then die.

Am I missing something very obvious in PHP v 5

Form

<body>
<form method="post" action="">  
  <p align="center" class="style1">
    <label>Username
    <input type="text" name="username" />
    </label>
  </p>
  <p align="center" class="style1">
    <label>Password
    <input type="password" name="password" />
    </label>
  </p>
  <p align="center" class="style1">
     <label>Remember me
       <input type="checkbox" name="rememberme" />
    </label>    
  </p>
  <p align="center">    
    <input type="submit" name="login" value="Log in" />   
  </p>
</form>
</body>

Code:

if ($_POST['login']){
    //get date
    $username=$_POST['username'];
    $password=$_POST['password'];
    $rememberme=$_POST['rememberme'];
}else{
    die("Please enter a valid username and password and click Submit");
    }

Recommended Answers

All 4 Replies

try using using isset in $_POST

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

Thank you to those who have offered help.

I'm not 100% sure, but it seems that the way I defined the start of a PHP string has a consequence on the code.

For example
<?
will not process my php code

<?php
will process my code but will throw errors such as "undefined index" when I declare a statement such as "if ($index){then}else(other)

<?PHP
will process all of my code.

Is there something specific I have set in my PHP.in to dictate this behavior, or is this very strange, meaning I have probably corrupted by PHP installation, and should re-install.

Again many thanks

Try this

<?php 
//connect to MYSQL database
include 'connection.php';
if($_POST){
         $username=$_POST['username'];
        $password=$_POST['password'];
        $rememberme=$_POST['rememberme'];

$result="SELECT * FROM  user_table WHERE username='$username' and password='$passowrd'";        

if($result != NULL){
            echo "You log in";
            // or you can use this header to redirect
            // header("location:index.php");
        }else{
        echo"Please enter a valid username and password and click Submit";
// or you can redirect to         // header("location:index.php?message=0");
        }
    }
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.