0

I am currently having some troubles adding animation between web page change. Could someone help me?

I have a following Javascript for animation (when clicking login, login form fades out):

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if(isset($_POST['submit'])) {
if(empty($_POST['username']) || empty($_POST['password'])) {
    $error = "Username or Password is invalid";
}
else {
    // Define $username and $password
    $username=$_POST['username'];
    $password=$_POST['password'];
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $connection = mysql_connect("localhost", "root", "");
    // To protect MySQL injection for Security purpose
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    // Selecting Database
    $db = mysql_select_db("phplogin", $connection);
    // SQL query to fetch information of registerd users and finds user match.
    $query = mysql_query("select * from users where password='$password' AND username='$username'", $connection);
    $rows = mysql_num_rows($query);
    if($rows == 1) {
        $_SESSION['login_user']=$username; // Initializing Session
        //header("location: home.php"); // Redirecting To Home Page
    } else {
        $error = "Username or Password is invalid";
    }
    mysql_close($connection); // Closing Connection
}

} ?>

This is the HTML form with an error field:

    <form class="form" action="" method="POST">
            <input id="username" name="username" type="text" placeholder="Username">
            <input id="password" name="password" type="password" placeholder="Password">
            <button name="submit" type="submit" id="login">
                Login
            </button>
            <span><?php echo $error; ?></span>
        </form>

Login.php to check if user data is correct/incorrect. Directs to a new page (home.php) on success:

if($rows == 1) {
        $_SESSION['login_user']=$username; // Initializing Session
        //header("location: home.php"); // Redirecting To Home Page
    } else {
        $error = "Username or Password is invalid";
    }

How can I enable click event so that on success it directs to a new page with fade out animation?

Changed .js to this:

$(document).ready(function() {
$('#loginbutton').click(function() {
    var username=$("#username").val();
    var password=$("#password").val();
    var dataString = 'username=' + username + '&password=' + password;
    if($.trim(username).length > 0 && $.trim(password).length > 0) {
        $.ajax({
            type: "POST",
            url: "login.php",
            data: dataString,
            cache: false,
            success: function(data) {
                if(data) {
                    $('.form').fadeOut(500, function() {
                        window.location.href = 'home.php';
                    });
                }
            }
        });
    } else {
        return false;
    }
});
});
techone
  • 41
  • 7
  • A side note, not verifying the input may result in SQL injection – Downgoat May 26 '15 at 22:28
  • Are you using a SPA router like angular ui.router? Or do you just want your entire page to fade out on success and then the page would fully reload? – Kevin F May 26 '15 at 22:29
  • Additionally, it'd be better to switch to `mysqli` (see http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). This is somewhat a duplicate question - there's [ajax methods](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call); or just do the fadeout, then redirect the user to Login.php using window.location ... – Andy Hoffner May 26 '15 at 22:35
  • When username and password are correct then clicking login button should make input from to fade out. After 1 second new page (home.php) should come up. Currently when I press login button, form fades out but nothing else happends since I have disabled button events. – techone May 26 '15 at 23:26

1 Answers1

2

You would need to make an ajax call to your php function, return whether the login is true or false, then animate if the php function returned true.

$.ajax({
    type: 'POST',
    url: 'Login.php?action=verify_login',
    data: formData,
    success: function(response){
        if(response.success){
            $('.form').fadeOut(1000, function(){
                window.location.href = 'new-url.php';
            });
        }
    }
});

If you mean to check that the form is valid as far as fields have text in them, then postback and let the server decide what to do you would:

    var $form = $('.form');
    var $username = $('#username');
    var $password = $('#password');

    //make this validation as fancy as you want
    if($username.val() !== '' && $password.val() !== ''){
        $form.fadeOut(1000, function(){
            $form.submit();
        });
    }

This will fade the form, submit the form after 1 second

Kevin F
  • 2,836
  • 2
  • 16
  • 21
  • I don't quite understand it. Fade animation is in login.js file where I had to add method event.preventDefault(). Checking user input is in login.php file, where I made method to check if input data is correct or not. What I want is to modify login.js so that when input data is correct, fade animation is called and 1 second after new page is loaded. – techone May 26 '15 at 23:31
  • 1
    The only way for your JS to know if the input data is correct is to run the logic on the server side. The only way to connect the client side to the server side without page load is ajax. You cannot verify the data in the js as it does not have direct access to your database and therefore has no way of knowing whether the data is correct. – Kevin F May 27 '15 at 00:11
  • Edited answer to show how to submit the form through jQuery to let php decide what to do. You would have to fill out the action="" on our form tag. I'm not sure how php handles it, but I imagine it would just be action="Login.php" – Kevin F May 27 '15 at 00:20
  • Edited my first post and put whole script there. I am getting parse error: syntax error, unexpected ')' (on line 27). – techone May 27 '15 at 05:48
  • Can you console.log(data) to see what it is? Everything seems to be in the right place as far as the syntax of your JS goes – Kevin F May 27 '15 at 05:52
  • Okey, I fixed it. In index.php I had
    . Just add to remove home.php from action. Animation is still not working.
    – techone May 27 '15 at 06:23
  • Have you added a breakpoint to see if it is getting past the if(data) part to even start the animation and that $('.form') is selecting something and not empty? – Kevin F May 27 '15 at 13:19