1

Like the title says, I'm having a problem regarding my validation php. It's just a simple login php, where if the user inputs the wrong information, a validation form will execute saying that it will return to the previous login page after the five seconds countdown. But, if the user inputs the right username and password, it will just display a welcome text on the validation php. However, whenever I run the program it executes both the welcome text AND the countdown, regardless of whether the inputted information is right or wrong.

Here's my first php:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style>
        body {
        margin:0; 
        font-family: Calibri}

        .style1 {
        color: #FFFFFF}

    </style>
</head>
<body>
    <h1>Getting Input from USER</h1><h2>EXAMPLE 1</h2>
    <form method="post" action="validatea.php">
    <table width="200" border="0" cellspacing="0" cellpadding="5">
        <tr>
            <th bgcolor="#006699" scope="row"><span class="style1">Username</span></th>
            <td><label>
            <input type="text" name="txtUsername" id="txtUsername" required="required"/>
            </label></td>
        </tr>

        <tr>
            <th bgcolor="#006699" scope="row"><span class="style1">Password</span></th>
            <td><label>
            <input type="password" name="txtPassword" id="txtPassword" required="required"/>
            </label></td>
        </tr>

        <tr>
            <th colspan="2" scope="row"><div align="right">
            <input type="reset" value="Clear"/>
            <input type="submit" value="Login"/>
            </div></th>
        </tr>
        </table>
        </form>
        <hr/>


</body>
</html>

Here's the Validation php:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

    <script>
        var ctr=5;
        function countdown(){
            window.document.getElementById("cnt").innerHTML=ctr;
            ctr--;
            if (ctr<0) window.location="samplea.php";
            setTimeout("countdown()", 1000);
        }
        </script>

</head>

<body>

<?php
    if(isset($_POST["txtUsername"])){
        $uname="user";$psw="hello";
            if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){
                echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>";
            else{
                echo"Username and Password is invalid.<br/><br/>";
                echo'This will redirect to login form in <label id="cnt"></label>seconds...';
                echo'<script> countdown();</script>';
            }
        }
    }
?>

</body>
</html> 
1
  • 1
    You're missing a closing brace before the else. Commented Sep 30, 2015 at 1:39

2 Answers 2

1

You missplaced the } in the second IF

<?php
    if(isset($_POST["txtUsername"])){
        $uname="user";$psw="hello";
        if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){
                echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>";
        }else{ //Forgot to close if }
                echo"Username and Password is invalid.<br/><br/>";
                echo'This will redirect to login form in <label id="cnt"></label>seconds...';
                echo'<script> countdown();</script>';
        }
    }
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Tried it. still gets the same output on the validation : Welcome " .$_POST["txtUsername"]."!"; } else{ echo"Username and Password is invalid. "; echo'This will redirect to login form in 2seconds...'; echo''; } } ?>
It's working fine to me. What exactly are you getting wrong?
are you using a .html file or .php?
Regardless of what I input, the validation still produces this output : Welcome " .$_POST["txtUsername"]."!"; } else{ echo"Username and Password is invalid. "; echo'This will redirect to login form in 2seconds...'; echo''; } } ?> The countdown still works, but it the whole validation displays like the output above, raw.
Either you don't have PHP installed, or you'are using a .html file instead of .php
|
0

You haven't closed your second if in Validation php! Close it and it will work fine

1 Comment

Tried it, but still the same. I think the code's not the problem... maybe it's the xampp or something. :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.