簡體   English   中英

PHP ajax不顯示數據庫中的任何數據

[英]PHP ajax not display any data from DB

我正在嘗試對一些ajax編碼進行編碼,當用戶輸入其名稱然后單擊提交時,與它們有關的所有數據將在不刷新頁面的情況下顯示。 但是我的問題是我的代碼不起作用(不顯示輸出)。 我能知道問題是什么嗎,也許可以給我一些解決方案/示例,謝謝。

下面是我的代碼:

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

  <script type="text/javascript">

  $(document).ready(function() {

  $("#display").click(function() {                

     $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
        $("#responsecontainer").html(response); 
        //alert(response);
                 }

            });
       });
    });

   </script>

   <body>
   <form method = Post onclick="display">
   <input type ="text" id='name'><br>
   <input type='submit'>
   </form>


   <h3 align="center">Manage Student Details</h3>

   <div id="responsecontainer" align="center"></div>
   </body>

php文件:

  <?php
   include("co.php");
   mysql_select_db("testing",$con);
   $result=mysql_query("select * from Login where user_name='".$_POST['name']."'",$con);

  echo "<table border='1' >
        <tr>
         <td align=center> <b>user Id No</b></td>
         <td align=center><b>Name</b></td>
         <td align=center><b>Password</b></td>
         <td align=center><b>responsibility</b></td></td>";

            while($data = mysql_fetch_row($result))
   {   
         echo "<tr>";
         echo "<td align=center>$data[0]</td>";
         echo "<td align=center>$data[1]</td>";
         echo "<td align=center>$data[2]</td>";
         echo "<td align=center>$data[3]</td>";
              echo "</tr>";
   }
        echo "</table>";
      ?>

co.php是我的config.file

在表單中,您使用的是onclick方法,當調用ajax時,可以使用#display作為ID,但這是方法,因此請刪除表單代碼並放入此代碼

<form method = Post id="display">
   <input type ="text" id='name'><br>
   <input type='submit'>
</form>

您需要將name變量發送到php頁面。 您的ajax請求應如下所示:

$.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",
        data: { name: $('#name').val() }, // set the naem variable
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response); 
            // alert(response);
        }

            });
       });
    });

編輯:您還需要使用Dhaval Gohel的答案。 有多個問題。

編輯:您還應該將.clcik更改為.submit。 這可能就是您要尋找的行為。

您的JavaScript看起來像這樣:

  <script type="text/javascript">

  $(document).ready(function() {

  $("#display").submit(function(e) {   
         e.preventDefault();

     $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "tesz2.php",
        data: { name: $('#name').val() }, // set the naem variable             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
        $("#responsecontainer").html(response); 
        //alert(response);
                 }

            });
       });
    });

   </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM