Ajax在PHP页面上不起作用

I am trying to understand the basics of using AJAX in conjunction with PHP in order to use php pages to provide functions, but not change my 'view' on my MVC design.

So I created this basic login page...

 <!DOCTYPE html>
    <head>
    <title>learning Php</title>


<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- Latest compiled and minified JavaScript -->

<script type="text/javascript">
$(document).ready(function() {

    $(#"login").click(function() {

        var action = $("#form1").attr("action");
        var form_data = {
            username: $("#username").val(),
            password: $("#password").val(),
            is_ajax: 1
        };

        $.ajax({
            type: "POST",
            url: action,
            data: form_data,
            success: function(response)
            {   
                if(response == 'success')
                {
                    $("#form1").slideUp('slow', function() {
                        $("#message").html('<p class="success">You have logged in.</p>');
                    };
                }
                else 
                    $("#message").html('<p class="error">Incorrect password or username.</p>');
            }

        });

        return false;
    });

});
</script> 

</head>
<body>
<div>
    <form name="form1" id="form1" method="post" action="loginForm.php">
        <p>
            <label for="username"> Username: </label>
            <input type="text" id="username" name="username" />
        </p>
        <p>
            <label for="password"> Password: </label>
            <input type="text" id="username" name="username" /> 
        </p>
        <p>
            <input type="submit" id="login" name="login" value="login" /> 
        </p>
    </form>
    <div id="message"></div>
<div>
</body> 
</html>

And this was my php page to "handle" to login...

<?php

   $is_ajax = $_REQUEST['is_ajax'];
   if(isset($is_ajax) && $is_ajax)
   {
       $username = $_REQUEST['username'];
       $password = $_REQUEST['password'];

       if($username == 'demo' && $password == 'demo')
       {
           echo 'success';
       }
   }
?>

The problem I am having is that whenever I submit my login, I am redirected to "/loginForm.php" instead of staying on my current page and having the message change underneath the login form.

I tried using Firebug to help me track down what I suspected to be a javascript error, but to no avail.

Any idea on why I am being redirected or why the form is not submitting via Ajax?

a small mistake

$(#"login").click(function() {

This should be

$("#login").click(function() {
  ^ // # inside quotes.

One more mistake here

if(response == 'success')
{
    $("#form1").slideUp('slow', function() {

    });  <--- You Missed ")" here 
}

Besides the typo and Rocky's good catch on the }); <--- You Missed ")" here

Both your username and password fields are the same.

<label for="username"> Username: </label>
<input type="text" id="username" name="username" />

and

<label for="password"> Password: </label>
<input type="text" id="username" name="username" />

the 2nd one should read as

<input type="text" id="password" name="password" />

In using everyone's answer, you will have yourself a working script.

  • Remember to hash your password once you go LIVE.

Edit sidenote: I've made a note below about using a button, rather than an input.


Here's a rewrite, just in case. However that input needs to be a <button>.

<!DOCTYPE html>
    <head>
    <title>learning Php</title>


<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<!-- Latest compiled and minified JavaScript -->

<script type="text/javascript">
$(document).ready(function() {

    $("#login").click(function() {

        var action = $("#form1").attr("action");
        var form_data = {
            username: $("#username").val(),
            password: $("#password").val(),
            is_ajax: 1
        };

        $.ajax({
            type: "POST",
            url: action,
            data: form_data,
            success: function(response)
            {   
                if(response == 'success')
                {
                    $("#form1").slideUp('slow', function() {
                        $("#message").html('<p class="success">You have logged in.</p>');
                    });
                }
                else 
                    $("#message").html('<p class="error">Incorrect password or username.</p>');
            }

        });

        return false;
    });

});
</script> 

</head>
<body>
<div>
    <form name="form1" id="form1" method="post" action="loginForm.php">
        <p>
            <label for="username"> Username: </label>
            <input type="text" id="username" name="username" />
        </p>
        <p>
            <label for="password"> Password: </label>
            <input type="text" id="password" name="password" /> 



<!--
Your original input
<input type="text" id="username" name="username" /> 
-->
        </p>


<button type="submit" id="login" name="login" />LOGIN</button>

<!--
Your original submit input. Don't use it
        <p>
            <input type="submit" id="login" name="login" value="login" /> 
        </p>
-->

    </form>
    <div id="message"></div>
</div>
</body> 
</html>
  • Your last div just before </body> was unclosed </div>, I've changed that above.

Additional edit from comments.

It seems that there was probably a space inserted somewhere and the use of trim() was the final nail to the solution.

response.trim();

A special thanks goes out to Jay Blanchard to have given us a helping hand in all this, cheers Sam!

References (TRIM):