-1

We have a login page which validates the username and password from our database, however we'd like it to display the invalid username and password message above the input fields rather than at the top of the page. We know we can edit and put the php code in the div to display it where it is, we put out headers and therefore as soon as we move the php from the top of the page it generates an error message. Is there an alternative to this? Or a way of using the headers differently? Our code is below and an image of what we want...

enter image description here

<?php
        error_reporting (E_ALL ^ E_NOTICE);
        $username = $_POST['usrname'];
        $pass_word = $_POST['pass_word'];
        if(($usrname=="")&&($pass_word==""))
        {
            //Do nothing as nothing has been posted......
        }
        else
        {
            $con = mysql_connect("localhost"," "," ");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("", $con);

            $result = mysql_query("SELECT * FROM `leicester_login` WHERE `user_name`='$username'", $con);
            $num_row = mysql_num_rows($result);
        }

        if($username=="")
        {
            //do nothing as nothing has been posted
        }
        else
        {
            if($num_row == 0)
            {
            echo "<p>The username <b>". $username ."</b> doesnt exist!</p>";
            }
            else
            {
                $return_pass = mysql_fetch_array($result);
                if($pass_word == $return_pass['pass_word'])
                {
                    session_start();
                    if(isset($_SESSION['username']))
                    {
                        //do nothing as session already exists...
                        header("Location:  ");
                    }
                    else
                    {
                        $_SESSION['username']= $username;
                        header("Location:  ");
                        exit;
                    }
                }
                else
                {
                    echo "<p>The password entered is incorrect!</p>";
                }
            }
        }
    ?>


    <html lang="en">
      <head>
      </head>

      <body>

        <div class="container">

    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin">
        <h2 class="form-signin-heading"><img src="" alt="Logo"/>Leicester</h2>
        <?php

    ?>
        <h6 class="form-signin-heading">You are not logged in, please login.</h6>

            <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">
            <input type="password" name="pass_word" id="pass_word" class="input-block-level" placeholder="Password">
        <center><button align="center" class="btn btn-large btn-primary" type="submit">Sign in</button></center>
    <a href="shires.php" class="btn btn-link">< Go Back</a>
          </form>
      </div> <!-- /container -->

      </body>
    </html>
j08691
  • 204,283
  • 31
  • 260
  • 272
Browno
  • 41
  • 1
  • 7

4 Answers4

1

Your if statement is wrong [There is no such $usrname variable on your code]

if(($usrname=="")&&($pass_word==""))

should be

if(($username=="")&&($pass_word==""))
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 1
    It works with/without the variable being usr/username. Thanks didn't realise the variables were different, so thankyou. But still hasn't answered our question. Thanks. – Browno Nov 06 '13 at 15:36
0

I recommend using Javascript: If you place a span in the logindiv at the place you want. e.g. that span has an id of 'errormessage'. Now you can use Javascript to place the message in the span. HTML:

   <h6 class="form-signin-heading">You are not logged in, please login.</h6>

      <span id="errormessage"></span>

        <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">

PHP:

  //an error has occurred...
  echo "<script>document.getElementById('errormessage').innerHTML='The password entered is incorrect!'</script>";

A benefit of using a span is that you can edit the layout of the errormessage using CSS. I hope this is an answer to your question.

Mark
  • 3,224
  • 2
  • 22
  • 30
0

Try this:

<?php
    error_reporting (E_ALL ^ E_NOTICE);
    $username = $_POST['usrname'];
    $pass_word = $_POST['pass_word'];
    if(($usrname=="")&&($pass_word==""))
    {
        //Do nothing as nothing has been posted......
    }
    else
    {
        $con = mysql_connect("localhost"," "," ");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("", $con);

        $result = mysql_query("SELECT * FROM `leicester_login` WHERE `user_name`='$username'", $con);
        $num_row = mysql_num_rows($result);
    }

    if($username=="")
    {
        //do nothing as nothing has been posted
    }
    else
    {
       $message = null; //create a container for the messages

        if($num_row == 0)
        {
        $message = "<p>The username <b>". $username ."</b> doesnt exist!</p>";
        }
        else
        {
            $return_pass = mysql_fetch_array($result);
            if($pass_word == $return_pass['pass_word'])
            {
                session_start();
                if(isset($_SESSION['username']))
                {
                    //do nothing as session already exists...
                    header("Location:  ");
                }
                else
                {
                    $_SESSION['username']= $username;
                    header("Location:  ");
                    exit;
                }
            }
            else
            {
                $message = "<p>The password entered is incorrect!</p>";
            }
        }
    }
?>


<html lang="en">
  <head>
  </head>

  <body>

    <div class="container">

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin">
    <h2 class="form-signin-heading"><img src="" alt="Logo"/>Leicester</h2>
    <?php

     //if there's something in the container, echo it out.

     if (!is_null($message)) {
      echo $message;
     }
?>
    <h6 class="form-signin-heading">You are not logged in, please login.</h6>

        <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">
        <input type="password" name="pass_word" id="pass_word" class="input-block-level" placeholder="Password">
    <center><button align="center" class="btn btn-large btn-primary" type="submit">Sign in</button></center>
<a href="shires.php" class="btn btn-link">< Go Back</a>
      </form>
  </div> <!-- /container -->

  </body>
</html>

Whenever echo is called it will output something right there, if it's before any output (like the html part), itt will show up before the HTML starts. If you put something in a container (like the $message) and output it with the html it will show up in the predestined place. As a side effect, the header("Location: "); will not throw headers already sent errors.

Meki
  • 395
  • 1
  • 7
  • 13
-1

It's outputting it up top because that is where you are telling it to output - before the actual start of the HTML. One way to solve it is to save the string to display in a variable, and then display the variable at the proper place in your HTML.

patricksweeney
  • 3,939
  • 7
  • 41
  • 54