我使用javascript提交表单但我无法在php页面中获取值

时间:2015-10-04 00:45:05

标签: javascript php jquery html ajax

我正在使用此代码提交表单

    $('.login-form input').keypress(function (e) {
        if (e.which == 13) {
            if ($('.login-form').validate().form()) {
            //$( "#login-form input" ).submit();
               // window.location.href = "check.php";
     $.post("check.php", $("#login-form input").serialize(),               function(data) {
     window.location.href = "check.php";
      });
            }
            return false;
        }
    });

我无法在check.php中获取值。请帮助我。我知道它是一个基本的东西,但我无法做到这一点。提前致谢

这是我的PHP代码

       <?php
       session_start();
       include_once("Includes/db_connection.php");
       include_once("participant/welcome.php");
       include_once("admin/admin_login.php");
       include_once("staff/staff_login.php");
       include_once("lecturer/lecturer_login.php");
       include("Includes/location.php");
       //include("Includes/lecturer_data_dropdown.php");

       $username=$_POST["username"];
       $password=$_POST["password"];
       echo $username;
       if($_POST["user_type"]=="participant")
       {
       participant($password); 
       }
       elseif($_POST["user_type"]=="admin")
       {
       admin($username, $password); 
       }
       elseif($_POST["user_type"]=="staff")
       {
       staff($username, $password); 
       }
       elseif($_POST["user_type"]=="lecturer")
       {
       lecturer($username, $password);
       }

       ?>

这里我只需要usertype,用户名和密码来做出登录决定

2 个答案:

答案 0 :(得分:2)

我注意到您在代码的一个部分中引用了$('.login-form')而在另一部分中引用了$('#login-form')。如果您的表单实际上使用的是类而不是ID,则.serialize()方法将返回一个空数组。将其更新为$('.login-form input')(请注意句点而不是哈希值)。

有关jQuery选择器的更多信息:https://api.jquery.com/category/selectors/

答案 1 :(得分:1)

试试这个我已经尝试过并且工作得很好:

var var_username=$("Id of input box for username").val();
var var_password= $("Id of input box for password").val();

$.post(URL,
  {
    username: var_username,
    password: var_password
  },
  success:function(data)
  {
      //your message here..
  }
);