必承其重 | 欲带皇冠 2015-11-24 17:17 采纳率: 0%
浏览 31

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?

  • 写回答

3条回答 默认 最新

  • 10.24 2015-11-24 17:18
    关注

    a small mistake

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

    This should be

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

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真