dongpang2483 2018-08-11 06:35
浏览 119
已采纳

将Ajax成功数据存储到javascript变量中以在其他js函数中使用

I am currently doing login form and I have database with the username and password. I've already done comparing if username and password is match with that username But I want to display username is not exist when I click the submit button using the variable come from ajax response. I've already done it but the variable is stored inside html tag.

Here is my old code

function checkUsn(){
    var usn = document.getElementById("usn").value;
    if(usn){
        $.ajax({
            type: 'post',
            url: 'checkdata.php',
            data: {
                emp_username: usn,
            },
            success: function(response){
                $('#status').html(receiveUsn);
                if (response == "OK"){
                    return true;
                }else{
                    return false;
                }
            }
        });
    }else{
        $('#status').html("");
        return false;
    }
}

I want something like this but this code below didn't work

function checkUsn(receiveUsn){
    var usn = document.getElementById("usn").value;
    if(usn){
        $.ajax({
            type: 'post',
            url: 'checkdata.php',
            data: {
                emp_username: usn,
            },
            success: function(response){
                var receiveUsn = response; //I want to use this for other function
                $('#status').html(receiveUsn);
                if (response == "OK"){
                    return true;
                }else{
                    return false;
                }
            }
        });
    }else{
        $('#status').html("");
        return false;
    }
}

Then I will use the variable come from ajax on this function

function checkall(receivePw, receiveUsn)
{
    if(receiveUsn == "OK" && receivePw == "MATCH"){
        return true;
    }else{
        return false;
    }
}

php code

<?php
include 'db_config.php';
$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);

if(isset($_POST['emp_username'])){
    $usn = $_POST['emp_username'];

    $checkdata = "SELECT emp_username FROM emp_details where emp_username='$usn'";

    $query = mysqli_query($conn, $checkdata);

    if(mysqli_num_rows($query) > 0){
        echo "OK";
    }else{
        echo "Your Username not exist";
    }
    exit();
}

if(isset($_POST['emp_pw']) && isset($_POST['emp_usn'])){
    $pw = $_POST['emp_pw'];
    $usn = $_POST['emp_usn'];

    $get_pw = "SELECT emp_password FROM emp_details where emp_username='$usn'";

    $query = mysqli_query($conn, $get_pw);

    //$get_num_rows = mysqli_num_rows($query);
    //echo $get_num_rows;

    $row = mysqli_fetch_assoc($query);
    //echo $row["emp_password"];

    // check if password is match with username
    if($pw == $row["emp_password"]){
        echo "MATCH";
    }else{
        echo "Wrong password";
    }
    exit();
}
?>

login form

<form class="modal-content animate" action="/login_action.php" method="post" onsubmit="return checkall();">
    <div class="container">
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span>
      <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"></div>
      <img class="avatar img-responsive col-lg-6 col-md-6 col-sm-6 col-xs-6" src="img/employee_avatar.png" alt="Avatar">
      <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3"></div>
    </div>
    <div class="container">
      <label for="usn"><b>Username</b></label>
      <input id="usn" type="text" placeholder="Enter Username" name="usn" onkeyup="getPw();checkUsn();" required>

      <label for="pw"><b>Password</b></label>
      <input id="pw" type="password" placeholder="Enter Password" name="pw" onkeyup="getPw();checkUsn();" required>

      <button type="submit">Login</button>
      <label>
        <input type="checkbox" checked="checked" name="remember"> Remember me
      </label>
    </div>

    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
      <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
    <span id="status">status</span><br>
    <span id="status2">status</span><br>
    <span id="status3">status</span>
  </form>

Please help Thank you!!!

  • 写回答

3条回答 默认 最新

  • 「已注销」 2018-08-11 07:05
    关注

    In a <script> tag you need to declare a variable at the root level, this will create the global variable. It's considered best practice not to pollute the global space so declare an object at the global level and use that to store your global variables.

    <script>
      var myGlobalContainer = {};
    </script>
    

    Now in the JavaScript code on the page you can use the myGlobalContainer object.

     // set a value
     myGlobalContainer.hello = "world";
    
     // read a value
     console.log(myGlobalContainer.hello);
     // "world"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能