0

I'm trying to create a log in form using html > ajax > php, the problem is the php is not working, I don't know where is the problem, I think the ajax cannot execute my php file. I need help. Thanks in advance.

Here is my HTML code: my form and inputs are below

          <form id="loginForm">
            <input type="text" data-clear-btn="true" name="username" id="username" value="" placeholder="Username / ID No.">
            <input type="password" data-clear-btn="true" name="password" id="password" value="" placeholder="Password">
            <input type="checkbox" name="rem_user" id="rem_user" data-mini="true">
            <label for="rem_user">Remember me</label>
            <input type="submit" name="login" id="login" value="Log in" class="ui-btn" />
          </form>
          <div class="err" id="add_err"></div>

AJAX script that sends request on my php file

    <script>
        $(document).ready(function(){
            $("#loginForm").submit(function(){
                var username = $("#username").val();
                var password = $("#password").val();
                // Returns successful data submission message when the entered information is in database.
                var dataString = 'username=' + username + '&password=' + password;
                    if (username == '' || password == ''){
                        alert("Please Fill All Fields");
                    }
                    else {
                    // AJAX Code To Submit Form.
                        $.ajax({
                            type: "POST",
                            url: "php/login-action.php",
                            data: dataString,
                            success: function(result){
                                window.location="#report_page";
                            }
                        });
                    }
                return false;
            });
        });
    </script>

PHP File

<?php

require "includes/connection.php";
include "includes/function.php";
if(isset($_POST['login'])){


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

    $username = sanitize($username);
    $password = sanitize($password);
    $pass2 = md5($password);
    $salt = "sometext";
    $validateHash = $salt.$pass2;
    $pass = hash("sha512", $validateHash);

    $sql = "SELECT * FROM user_login WHERE username='".$username."' and password='".$password."'";
    $result = mysqli_query($con,$sql) or die("Error: ". mysqli_error($con));

    $count=mysqli_num_rows($result);

    while($row=mysqli_fetch_array($result))
        {
            $id = $row['user_id'];
            $username = $row['username'];
            $name = "".$row['firstname']." ".$row['lastname']."";
            $acc_type = $row['Acc_Type'];
        }
        if($count==1){
            if($acc_type == 'user') {
                $_SESSION["id"] = $id;
                $_SESSION["username"] = $username;
                $_SESSION["name"] = $name;
                echo 'true';
            }
            else {
                echo 'false';
            }

        }


}
?>
1
  • 3
    dataString = 'login=login'....... Commented Nov 27, 2014 at 2:46

2 Answers 2

1

as Cattla mentioned in comments.

Your PHP is looking for $_POST['login'], and your $.ajax call didn't pass that in.

so here is the answer

var dataString = 'login=login&username=' + username + '&password=' + password;

Debug tips

  • Did ajax send all required inputs to PHP (you can inspect this from browser developer tool)
  • Did php receive all required inputs (you could var_dump($_POST)
  • Did php connect to mysql successfully
  • Did ajax receive data from PHP (use alert or console.log)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I tried your answer but it didn't work in my code. Do you think I still have error on my PHP?
the error could be anywhere, you need to debug it step by step, from ajax to PHP
Thanks for your tips. When ever I fix it I'll update my post.
0

try this, and if you get error state what it is

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
        $(document).ready(function(){
            $("#loginForm").submit(function(){

                    if (username == ' ' || password == ' '){
                        alert("Please Fill All Fields");
                    }
                    else {
                    // AJAX Code To Submit Form.
                        $.ajax({
                            type: "POST",
                            url: "php/login-action.php",
                            data: $(this).serialize(),
                            success: function(result){
                               alert('sucess'); //window.location="#report_page";
                            }
                        });
                    }
                return false;
            });
        });
    </script>

1 Comment

Just replace only your jquery in head tag

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.