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 461593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:59:24+00:00 2026-05-12T22:59:24+00:00

I want to use AJAX to process a simple login form. I thought it’d

  • 0

I want to use AJAX to process a simple login form. I thought it’d be pretty easy, but I just can’t get it all to work.

index.php

<html>
<head>
<title>AJAX Login</title>
<script type="text/javscript">
var XMLHttpRequestObject = false;

if(window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

    function logIn() {
        if(XMLHttpRequestObject) {
            var obj = document.getElementById("show");
            XMLHttpRequestObject.open("GET", "login.php");

            XMLHttpRequestObject.onreadystatechange = function() {
                if(XMLHttpRequestObject.readyState == 4
                   && XMLHttpRequestObject.status == 200) {
                        obj.innerHtml = XMLHttpRequestObject.responseText;
                    }
                }

                XMLHttpRequestObject.send(null);
            }
        }

</script>
</head>
<body>
<form action="" method="post">
<table>
    <tr>
        <td>Username:</td>
        <td><input type="username" name="username" /></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><input type="password" name="password" \ /></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="login" onclick="logIn();" /></td>
    </tr>
</table>
</form>
<div id="show">should go here</div>
</body>
</html>

login.php

<?php
$username = "andrew";
$password = "andrew";

if($_POST['username'] != "") {
    if($_POST['password'] != "") {
        if(($_POST['username'] == $username) && ($_POST['password'] == $password)) {
            echo "Login Success!";
            }
            else {
                echo "Login Failure!";
                }
            }
            else {
                echo "You didn't enter a password";
                }
            }
            else {
                echo "You didn't enter a username";
                }
?>

When I click the “Login” button, nothing happens. 🙁

  • 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-05-12T22:59:24+00:00Added an answer on May 12, 2026 at 10:59 pm

    When you click login on that form it submits back to the source URL. To prevent this you should really change your event handler to either this:

    function logIn() {
      ...
      return false;
    }
    

    Alternatively you can do it from the submit handler on the form instead of the click handler on the submit button.

    Secondly, to do AJAX cross-browser you need about 6 fallback conditions, not just XmlHttpRequest. See Getting Started with AJAX and the XMLHttpRequest Object.

    In all honesty, for AJAX there’s no way I’d consider doing it without a helper library. My preferred choice is jQuery, in which case the code ends up looking like this:

    <form id="loginform" method="post">
    <table>
      <tr>
        <td>Username:</td>
        <td><input type="username" id="username" name="username" /></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" id="password" name="password" \ /></td>
      </tr>
      <tr>
        <td colspan="2"><input type="button" id="login" value="Login"/></td>
      </tr>
    </table>
    </form>
    

    with:

    <script type="text/javascript">
    $(function() {
      $("#login").click(function() {
        $("#show").load("login.php", {
          username: $("#username").val(),
          password: $("#password").val()
        }, function() {
          $("#loginform").hide(); // for example
        });
      });
    });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the Auth component. I have an Ajax login form and I want
I want to use the new combobox control of the Ajax control Toolkit. But
I want to create a simple AJAX call, based on How to use Servlets
I'm basically just trying to submit a form through AJAX and want to show
I want to use Goolge AJAX Feed API in my C# console application to
I want to use the ajax dll 3.5 in the website of framework 2.0
I have an asp.net user control on which I want to use jquery ajax.
I want to start an application which will use ajax push, however the web
I want to settle on a GUI framework, and use AJAX, as simply as
Possible Duplicate: How to use Basic Auth and Jquery and Ajax I want to

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.