0

hello I am new here and in programming as well. Today I have been made a login form in php / mysql and jquery form validation and was surprised that my form do not work bacause of js script? withou work well.

<div class="top_login_error" id="top_error">All fields must be field</div>
    <div id="logincontainer">
        <div id="loginbox">
            <div id="loginheader">
                <div>Control Panel Login</div>
            </div>
            <div id="innerlogin">
                <form method="POST" action="obr.php" id="login_form">
                    <p>Enter your username:</p>
                    <input type="text" class="logininput" name="login" id="login" />
                    <p>Enter your password:</p>
                    <input type="password" class="logininput" name="password" id="password" />
                    <input type="submit" class="loginbtn" value="Submit" name="enter" id="admin_login" /><br />
                    <p><a href="#" title="Forgoteen Password?">Forgotten Password?</a></p>
                </form>
            </div>
        </div>
        <img SRC="../images/administration/login_fade.png" alt="Fade" />
    </div>
</body>

jquery ccode for validating form

$('#admin_login').click(function(q){

q.preventDefault();

    var login = $('#login').val();
    var password = $('#password').val();

    if (login.length == 0){
        $('#top_error').fadeIn(500);
        $('#login').addClass("logininput_error");
        var error = true;
    } else {
        $('#top_error').fadeOut(500);
        $('#login').removeClass("logininput_error");
        }
    if (password.length == 0){
        $('#top_error').fadeIn(500);
        $('#password').addClass("logininput_error");
        var error = true;
    } else {
        $('#top_error').fadeOut(500);
        $('#password').removeClass("logininput_error");
        }

});

and php

<?php 
session_start(); 



    if (isset($_POST['login'])){$login = $_POST['login'];} if ($login == ''){unset($login); exit('please enter name');}
    if (isset($_POST['password'])){$password = $_POST['password'];} if ($password == ''){unset($password); exit('please enter password');}

    $login = mysql_escape_string($_POST['login']);
    $password = mysql_escape_string($_POST['password']);

    //$password = md5($password); I have been disabled while testing in simle way, login : aaa , password : aaa;

    include("../db.php");

    $result = mysql_query("SELECT * FROM users WHERE login='".$login."' and password='".$password."'") or die(mysql_error());

    $line = mysql_fetch_array($result);

    if (empty($line['id'])){
        exit("Your name or login is incorect <a href='login.php'>back</a>");
    } else {

    $_SESSION['autorized'] = true;
    $_SESSION['login'] = $login;
    $_SESSION['password'] = $password;
    }
    echo "<html><head><meta http-equiv='Refresh' content='0; URL=index.php'></head></html>";

?>

Can someone help me with code, tell what to change that everything work fine. Thank you

2 Answers 2

5

The culprit is q.preventDefault();, only do that when the form shouldn't be submitted.

Sign up to request clarification or add additional context in comments.

1 Comment

If it is not to hard maybe can someone tell me what code add to my jquery to send data with ajax
1

Probably Prusse is right.

the Jquery documentation is very good => You should read http://api.jquery.com/jQuery.post/ to do post request(AJAX) and http://api.jquery.com/submit/ to handle submit.

I would also advice you to use PDO instead of "normal" MySQL to also prevent against SQL-injections. You can then use PHPunit to properly test your code following Uncle Bob's three rules of TDD. I like to use in-memory SQLite database to test my code(new PDO('sqlite::memory:');). You could see my simple authentication library to see how I did that => https://github.com/alfredwesterveld/php-auth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.