0

I'm trying to make a simple login script and I've started with just trying to verify an email address. Essentially, from my login screen I am entering a valid email address and submitting it which should run AJAX to run a PHP function. I've scoured the internet all of last night and this morning trying different methods but I keep hitting dead ends.

To keep it brief here is the relevant code:

app.js

$(document).ready(function() {
    $("submit").click(function() {
        alert("This was a test.");
        $.ajax({
            type: "POST",
            url: "main_login.php",
            success: function(data) {
                alert(data);
                $(".message").text(data);
            }
        });
    });
});

main_login.php

<?php
    function checkLogin()
        $conn = new mysqli('localhost', 'user', 'pass!', 'db');
        mysqli_set_charset($conn, 'utf8mb4');
        $check = "SELECT * FROM users WHERE email = '$_POST[email]'";
        $results = mysqli_query($conn, $check);
        $data = mysqli_fetch_array($results, MYSQLI_NUM);
        if($data > 1) {
            echo "Login successful";
        } else {
            echo "This account does not exist";
        }

        $conn->close();
    }
?>

login.php

<div class="container login">
    <div class="panel panel-default login-panel">
        <div class="panel-heading">span class="log-h1">Sign-in</span>
        </div>
        <div class="panel-body">
            <form name="loginform" action="login.php" method="post">
            <div class="form-group">
                <label for="username">Email</label>
                <input type="email" class="form-control"
                 name="email" id="email" placeholder="Email address">
            </div>
            <div class="form-group" style="float: right;">
                <input type="submit" class="btn btn-primary" name="Submit" 
                 value="Sign-in">
            </div>
            </form>
            <span class='message'></span>
        </div>
    </div>
</div>

I'm not getting anything happening. The page notifies me when I refresh it that it may resend data but when i submit my email it doesn't do anything.

9
  • Any errors showing? Did you check the console (CTRL + F12 on Chrome)? Commented Oct 24, 2015 at 14:21
  • The errors I did have I just fixed and tried again but to no avail. When I hit the submit button it does reload the page; however, I don't get anything else other than a refresh it looks like. Commented Oct 24, 2015 at 14:27
  • You're using mysqli_fetch_array to get your data from your recordset, but there's no error checking to make sure that your query has worked. Also, you can simplify your code - if ($row = mysqli_fetch_array($results)) will return false if there are no matching results Commented Oct 24, 2015 at 14:37
  • Try using preventDefault() at beginning of jQuery submit binding? Commented Oct 24, 2015 at 14:40
  • use event.preventDefault() for stop reload page Commented Oct 24, 2015 at 14:43

1 Answer 1

1

It maybe due to the fact that your not preventing the default form behaviour of "submit", despite binding to the click event via jQuery, try this...

JS

$(document).ready(function() {
    $("#submit-button").click(function(e) {
        e.preventDefault(); 
        alert("This was a test.");
        $.ajax({
            type: "POST",
            url: "main_login.php",
            success: function(data) {
                alert(data);
                $(".message").text(data);
            }
        });
    });
});

HTML

<input id="submit-button" type="submit" class="btn btn-primary" name="Submit" value="Sign-in">
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting the same results.
Are you getting any errors in the console inspector?

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.