Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:27:29+00:00 2026-06-05T04:27:29+00:00

I am having problems with an ajax login form. I am getting an a

  • 0

I am having problems with an ajax login form. I am getting an a error from ajax saying that my username and password variables in my login_check.php file are undefined. Yet, when I initialize the variables to blank, the error goes away. The problem is, when I initialize the variables to blank, even if I put the right information into the login box, it will not let me log in. I am sure the problem is minor, but I just can’t see it. I just need the PHP and AJAX to work together and then I’m done. Thanks in advance.

login_check.php

<?php
require_once 'config.php';
require_once 'login.class.php';
$error = NULL;
$success = FALSE;

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

$login = new Login();
//Connect to MYSQL Database server
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Could not connect to MYSQL Database.");
$result = mysql_select_db(DB_NAME, $connect) or die("Could not connect to MYSQL table.");

//Clean Data to prevent malicous injections
$username = mysql_real_escape_string(strip_tags(stripcslashes(trim($_POST['username']))));
$password = $login -> encrypt(mysql_real_escape_string(strip_tags(stripcslashes(trim($_POST['pass'])))));

$sql = mysql_query("SELECT * FROM admin WHERE username = '$username' AND password = '$password'") or die("Query to database failed.");
$numrows = mysql_num_rows($sql);

$row = mysql_fetch_array($sql);
if ($numrows > 0) {
    session_start();
    $_SESSION['username'] = $row['username'];
    $success = TRUE;
    echo json_encode(array("success" => "Login successful."));

} else {

    $success = FALSE;
    echo $error = 'Invalid username or password!';
    echo json_encode(array("error" => "Invalid username or password! (PHP response)"));

}
?>

JQuery file

$('#login_form').submit(function(e) {

        e.preventDefault();

        var username = $('.username').val();
        var password = $('.password').val();

        $.ajax({
            type : "POST",
            url : "includes/login_check.php",
            data : "Username=" + username + "&Password=" + password,
            dataType : 'json',
            cache : false,
            success : function(data) {
                if(data.error) {
                    $('.login div.error').show().html(data.error);

                } else {
                    $('.login div.success').show().html(data.success);
                }

                //return false;

            },
            error : function(jqXHR, textStatus, errorThrown) {
                alert("error " + textStatus + ": " + errorThrown);
            },
            beforeSend : function() {
                $(".load").html("Loading...");
            }
        });

    });

By the way, the error from AJAX is:

error parsererror: SyntaxError: JSON.parse: unexpected character

Thank you again friends.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:27:32+00:00Added an answer on June 5, 2026 at 4:27 am

    Several problems:

    This:

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

    should be this:

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

    Those keys have to match the parms you specified here:

    data : "Username=" + username + "&Password=" + password,
    

    …and even that should be changed to:

    data : {"Username":username,"Password":password},
    

    This line is producing invalid JSON:

    echo $error = 'Invalid username or password!';
    

    I think you meant to code just this?

    $error = 'Invalid username or password!';
    

    EDIT #1:

    Also this line needs to be the very first thing in the PHP code:

    session_start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now that I was helped getting AJAX running just great, I'm having problems running
I am having problems posting this multi checkbox data with ajax. <?php foreach($_SESSION['contacts'] as
I am having problems with jQuery Ajax and PHP I have my php file
I'm having some problems generating an AJAX response with PHP. The response should be
I am having problems passing my model from my view via an Ajax call
I have my jquery ajax tabs running great. However I having problems getting/setting the
I'm having problems with understanding how jQuery create headers for ajax post. I have
I'm having some problems adding a dropdownlist with ajax and stuff using jQuery. When
I've got a page with ajax links between pages, and I'm having some problems
I am having problem in getting reference of Ajax control extender using $find() or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.