首页 > 解决方案 > 我应该使用什么来从 php 中的用户 ID 和密码字段中获取值?我得到一个带有给定代码的空字符串

问题描述

我必须归档 main.html(主文件)和 login.html(没有 html 文档的仅包含正文和 html 标记的表单),并使用 jquery 的 .load() 方法将表单从 login.html 加载到主文件。 html 并提交到 login.php。当我尝试在 php 中获取输入字段的值时,它只会给出一个空字符串或什么也不说。我不想只使用这种方法。那么它是错误的还是不可能的,或者是否有任何其他 php 方法能够获取这些字段。

main.html

    <html>
    <head>
        <title>My Media</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <!--script src="https://code.jquery.com/jquery-1.11.1.min.js">   </script>
        <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
        <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0    /additional-methods.min.js"></script-->

        <script src="jquery.js"></script>
        <script src="javascript.js"></script>
        <link rel="stylesheet" href="css.css">
    </head>
    <body>
        <div id="nav-placeholder" name="nav-placeholder"></div>
        <div id="body" name="body">
        </div>

    </body>
</html>

登录.html

<div id="loginPage">
    <br><h1>My Media</h1><br><br><br>
    <form id="form1" name="form1" method="post" onsubmit="return validate()" action="login.php">
        UserId:<br><input id="userid" name="userid" type="text" required><br>
        Password:<br><input id="password" name="password" type="password" required><br>
        <input id="Login" name="Login" type="submit" value="login">
    </form> 
</div>

jQuery.js

$(function(){
    var page="home";
                $("#nav-placeholder").load("menu.html");    
                $("#body").load("home.html")

    $("body").on("click","#home",function(){
        if(page!="home"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").load("home.html");
        page="home";}
    }); 

    $("body").on("click","#help",function(){
    if(page!="help"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").load("help.html");
        page="help";}
    }); 

    $("body").on("click","#login",function(){
        if(page!="login"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").load("login.html");
        page="login";}
    }); 

    $("body").on("click","#register",function(){
        if(page!="register"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").load("register.html");
        login="register";}
    });

    $("body").on("click","#about",function(){
        if(page!="home"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").load("about.html");
        page="about";}  
    });

    $("body").on("click","#bottom",function(){
        if(page!="home"){
        $("#body").empty();//after("<h3><u>Some appended text.</u></h3>");
        $("#body").focus("#contacts");
        page="about";}  
    });

});

登录.php

<?php

$usrname = $_POST["userid"];
$pass = $_POST["password"];

echo $usrname."--".$pass;
?>

应该有什么用?

标签: phpjquerypost

解决方案


@Ashitosh Kamble

当用户单击登录时,您应该使用 ajax 调用以 json 格式发送表单值。

var userid = $('#userid').val();
var password = $('#password').val();

var data = {
  "user_id":userid,
  "homeTown": password
}

$("#login").click(function(event){
       $.ajax( {
                  url:'login.php',
                  type:'post',
                  success:function(data) {
                     // Allow Login
                  }
               });
            });

希望它有效!


推荐阅读