0

I am writing a simple login script in PHP with a form. But can't get it to work. There is probably something wrong with my AJAX. When i click the submit button it doesn't do anything.

This is my login HTML form:

<form id="login_form">
            <input type="text" class="input_field" name="username" id="username" value="gebruikersnaam" onclick="if(value=='gebruikersnaam'){ this.value='' }"/>
            <input type="password" class="input_field" name="password" id="password"/>
            <input type="submit" class="login_button" id="login_button" value="Ga door"/>
        </form>

This is my AJAX:

$(document).ready(function(){

    $("#login_form").submit(function() {

     $.ajax({
           type: "POST",
           url: 'php/login_check.php',
           data: $("#login_form").serialize(), // serializes the form's elements.
           succes: function(logincheck) {
                if (logincheck == 'true') {alert ("Succes");}
                else {alert ("Failed");}
           }
         });

    return false; // avoid to execute the actual submit of the form.
});
});

This is my PHP:

<?php
session_start();

include 'config.php';

$username = $_POST['username'];
$password = $_POST['password'];

//Check for credentials
$Credentials_Query = mysqli_query ($mysqli, "SELECT * FROM Registered_Users WHERE Username='$username' AND password='$password'"); 

$num_row = mysqli_num_rows($Credentials_Query);
$row=mysqli_fetch_array($Credentials_Query);
if( $num_row >= 1 ) { 
echo 'true';
$_SESSION['username_session']=$row['Username'];
}
else {
echo 'false';
}
?>

I know this is really basic stuff. Still I can't get it to work. Don't know what I'm missing...?

NvdB31
  • 302
  • 1
  • 3
  • 13
  • open up developer tools and click on the 'Network' tab.. do you see the request actually going out when you submit the form? – Jeff Lambert Aug 18 '14 at 20:00
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 18 '14 at 20:01
  • 3
    succes spelling is wrong it should be success – Kumar Saurabh Sinha Aug 18 '14 at 20:01
  • Please [learn to love labels](http://www.456bereastreet.com/archive/200711/use_the_label_element_to_make_your_html_forms_accessible/). Don't abuse the default value as one. – Quentin Aug 18 '14 at 20:02
  • @Watcher, Yes the request goes out. The PHP script works as expected. Outputs 'true' when credentials are correct. – NvdB31 Aug 18 '14 at 20:06
  • You're writing a login form but don't know enough to prevent sql injection or avoid plaintext passwords! Stop! Do not pass go! Instead, use someone else's open source & heavily vetted login system library! Learn how they do it first. Do not wing authentication, it won't end well. http://www.php-login.net/ just for example. – Kzqai Aug 18 '14 at 20:11

1 Answers1

1

You misspelt success as succes.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335