第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用AJAX從數(shù)據(jù)庫中檢索數(shù)據(jù)并將結(jié)果保存在變量中?

如何使用AJAX從數(shù)據(jù)庫中檢索數(shù)據(jù)并將結(jié)果保存在變量中?

PHP
慕運(yùn)維8079593 2022-08-05 10:20:41
我是jQuery和AJAX的新手,我正在將登錄頁面作為項(xiàng)目進(jìn)行處理,我需要使用AJAX從數(shù)據(jù)庫中檢索數(shù)據(jù)。我不是100%流利的英語,所以我會盡我所能解釋這個問題(在谷歌翻譯的幫助下)。以下是我正在使用的代碼:索引.html<!DOCTYPE html><html>  <head>  <title>Login</title>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>  </head>  <body>    <form validate="">      <input type="text" placeholder="Username" id="username" required/><br />      <input type="password" placeholder="Password" id="password" required/><br />      <input type="submit" id="submit" value="Login" />    </form>    <script type="text/javascript">    // when document is loaded    $(document).ready (      // when submit is clicked      $("#submit").click (        // sets test to null        var test = null;        // sets username to value of username input        var username = document.getElementById("username").value;        // AJAX request         $.ajax({          type: "POST",          async: true,          url: test.php,          data: {username: username},          success: function (data) {            test = data;            console.log(test);            return test;          }        });      );    );    </script>  </body></html>測試.php<?php// connects to database$conn = mysqli_connect('server', 'username', 'password', 'database');// sets var username to POST username value$username = $_POST['username'];// SQL Query$sql = "SELECT * FROM users WHERE username='" . $username . "'";$result = mysqli_query($conn, $sql);// sets result to mysqli_fetch_assoc()$result = mysqli_fetch_assoc( $result );// echos $resultecho $result['password'];// closes database connectionmysqli_close( $conn );?>控制臺日志控制臺輸出:“”[DOM] 輸入元素應(yīng)具有自動完成屬性(建議:“當(dāng)前密碼”):(更多信息:https://www.googlesite.com)未捕獲的語法錯誤:意外的令牌 var ajax.html:19I've looked at the code and I can't seem to find an error.Thanks in advance! ;)>P.S.>It's probably going to end up being some stupid typo.>Other than that, have a great day!
查看完整描述

2 回答

?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個贊

您可以使用 不使用事件。clicksubmit


在你的情況下,只要給你的喜歡 -idform


<form validate="" id="submit">

現(xiàn)在,在您的腳本中 -js


$(function() { //shorthand document.ready function

    $('#submit').on('submit', function(e) { 

        e.preventDefault();  //prevent form from submitting

        console.log(data);

        $.ajax({

          type: "POST",

          async: true,

          url: test.php,

          data: $(this).serializeArray(),

          success: function (data) {

            console.log(data);

          }

        });

    });

});

因此,請檢查您的整個代碼 -


<!DOCTYPE html>

<html>

  <head>

  <title>Login</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

  </head>

  <body>

    <form validate="" id="submit">

      <input type="text" placeholder="Username" id="username" required/><br />

      <input type="password" placeholder="Password" id="password" required/><br />

      <input type="submit" value="Login" />

    </form>

    <script type="text/javascript">

    // when document is loaded

    $(function() { //shorthand document.ready function

    $('#submit').on('submit', function(e) { 

        e.preventDefault();  //prevent form from submitting

        console.log(data);

        $.ajax({

          type: "POST",

          async: true,

          url: test.php,

          data: $(this).serializeArray(),

          success: function (data) {

            console.log(data);

          }

         });

        });

     });

    </script>

  </body>

</html>

希望這會幫助你。


查看完整回答
反對 回復(fù) 2022-08-05
?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個贊

您需要將一個函數(shù)傳遞給您的 document.ready() 調(diào)用和您的 click() 調(diào)用。


     <script type="text/javascript">


        $(document).ready(function() {

            Your variables here...


            $('#submit').click(function() {

                ... Ajax call here.

            });

        });

    </script>


查看完整回答
反對 回復(fù) 2022-08-05
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號