【问题标题】:I am submitting form using javascript but I am not able to get values in php page我正在使用 javascript 提交表单,但我无法在 php 页面中获取值
【发布时间】:2015-10-04 00:45:05
【问题描述】:

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

    $('.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);
       }

       ?>

在这里我只需要用户类型、用户名和密码来做出登录决定

【问题讨论】:

  • 您正在使用$.post(POST 动词)方法发布并立即重定向使用GET 动词的用户,您期望什么?
  • 我想重定向到该页面并在 check.php 中获取表单数据,我将在其中做出重定向决定。请帮我。我被卡住了

标签: javascript php jquery html ajax


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个,我试过了,效果很好:

    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..
      }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2011-10-10
      • 2017-06-21
      相关资源
      最近更新 更多