为什么这个jQuery函数与我的php文件无法正常通信?

时间:2013-06-27 06:25:20

标签: php jquery forms

让我告诉你我的所作所为:

(1)表格:

 <form name="login-form" class="login-form" method="post" onSubmit="login()">

  <div class="header">
    <h1>Sign In</h1>
  </div>

  <div class="content">
    <input type="hidden" name="siteToken" value="$token" />
    <input id="username" name="username" type="text" class="input username"  placeholder="Username" required="required" />

  <div class="user-icon"></div>
    <input id="password" name="password" type="password" class="input password" placeholder="Password" required="required" />

  <div class="pass-icon"></div>

  </div>

  <div class="footer">
    <input type="submit" name="submit" value="Login" class="button" />
  </div>
</form>

(2)jQuery功能:

$(document).ready(function login() {
  $('.login-form').submit(function() {
      var formData = $(this).serialize();
      $("input").prop("disabled", true);
      $.post('VRC_LoginProcess.php', formData, loginMessage);

      function loginMessage(data) {
        $('.header').append(data);
      };    
  });
});

(3)PHP功能:

<?php

require_once('VRC_Header.php');
require_once('../Classes/VRC_MasterOO.php');
require_once('../Classes/VRC_Secure_Login.php');


//*******************************//
//Declerations

$signIn = "";
$username = "";
$password = "";
$success = "";
$error = "";

//******************************//

//****************************************************************************************//
//Script Header

$signIn = new UserService($dbuser, $dbpass, $dbhost, $dbname); //Create new class instance 
$signIn->sec_session_start(); //Begin session

//***************************************************************************************//

//***************************************************************************************//
//Begin Login Functions

if(isset($_POST['username'], $_POST['password'])) {

    //Assign POST submissions to passable php variables
    $username = $_POST['username'];
    $password = $_POST['password'];
    $passedToken = $_POST['siteToken'];

    /*//Check Token Values (prevent CSRF attacks)
    if($passedToken != $_SESSION['token']) {
        $error = "CSRF attack detected. Please close your browser and try again."; 
        $signIn->csrfAttackLog($username);
        echo $error;
        exit();     
    }*/


    //Test if both fields are not null
    if($username == "" || $password == "")
    {
        $error = "Not all fields were entered<br />";
        echo $error;
        exit();
    }

    //Start login process
    else
    {
        $success = $signIn->login($username, $password);
        if ($success === true)
        { //Login Successful
            echo "Success!"; //Direct to main page.
            exit();
        }
        //Specific login failure determination
        else 
        {
            switch ($success){
                case 1:
                    $error = "Your account has been locked.";
                    echo $error;
                    break;
                case 2: 
                    $error = "Invalid Username/Password (2)";
                    echo $error;
                    break;
                case 3:
                    $error = "Invalid Username/Password";
                    echo $error;
                    break;  
                case 4: 
                    $error = "Invalid Username/Password (3)";
                    echo $error;
                    break;
            }
        }           
    }
}

&GT;

Fist off,我怀疑问题出在PHP函数中。我在实现jQuery调用之前测试过它(我直接在html action属性中使用它)。我怀疑这个问题是在jQuery函数中发生的(我刚开始使用jQuery并且我并不熟悉它)。

请注意,我已经删除了php文件中的令牌输入。我只是想在处理之前让它工作(这个部分还有另一个问题)。

我不相信post变量正确地发送到php文件。此外,我不相信我的jQuery函数,因为它正确地从我的php函数接收echo响应,在某种意义上它将显示为html - 只要它开始工作。

赞赏任何意见。

2 个答案:

答案 0 :(得分:4)

$(document).ready(function login() {

那条线就是问题。

该页面的其余部分可能无法使用登录功能。它只是匿名函数的名称,它是$(document).ready函数的参数。

您有范围问题。

单独移出登录功能:

function loginMessage(data) {
    $('.header').append(data);
};
function login() {
    $('.login-form').submit(function () {
        var formData = $(this).serialize();
        $("input").prop("disabled", true);
        $.post('VRC_LoginProcess.php', formData, loginMessage);
    });
}
$(document).ready(function () {
    login();
});

修改

在您的登录功能中,您正在为登录表单的提交事件注册处理程序。它必须只注册一次。

So, remove `onsubmit=login()` from your form's attributes, and you are good.

答案 1 :(得分:3)

稍微将您的HTML更改为

<form name="login-form" class="login-form" method="post">
    <div class="header">
         <h1>Sign In</h1>

    </div>
    <div class="content">
        <input type="hidden" name="siteToken" value="$token" />
        <input id="username" name="username" type="text" class="input username" placeholder="Username" required="required" />
        <div class="user-icon"></div>
        <input id="password" name="password" type="password" class="input password" placeholder="Password" required="required" />
        <div class="pass-icon"></div>
    </div>
    <div class="footer">
        <input type="submit" name="submit" value="Login" class="button" />
    </div>
</form>

和Javascript

$(document).ready(function () {
    $('.login-form').submit(function (e) {
        e.preventDefault();
        var formData = $(this).serialize();
        $("input").prop("disabled", true);
        $.post('VRC_LoginProcess.php', formData, loginMessage);

        function loginMessage(data) {
            $('.header').append(data);
        }
    });
});