简体   繁体   English

PHP ajax不显示数据库中的任何数据

[英]PHP ajax not display any data from DB

i'm trying coding some ajax coding that when user enter their name then click submit, all the data that related with them will be display without refreshing page. 我正在尝试对一些ajax编码进行编码,当用户输入其名称然后单击提交时,与它们有关的所有数据将在不刷新页面的情况下显示。 But my problem is my code doesn't work(don't show output). 但是我的问题是我的代码不起作用(不显示输出)。 can i know what the problem is and maybe give me some solution/example thanks. 我能知道问题是什么吗,也许可以给我一些解决方案/示例,谢谢。

below is my code: 下面是我的代码:

<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 File: 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 is my config.file co.php是我的config.file

in you form you are use onclick method and when call ajax you can use #display as id but it is method so remove your form code and put this code 在表单中,您使用的是onclick方法,当调用ajax时,可以使用#display作为ID,但这是方法,因此请删除表单代码并放入此代码

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

You need to send the name variable to the php page. 您需要将name变量发送到php页面。 Your ajax request should look like this: 您的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);
        }

            });
       });
    });

Edit: You also need to use Dhaval Gohel's answer. 编辑:您还需要使用Dhaval Gohel的答案。 There was more than one problem. 有多个问题。

Edit: You also should change you .clcik to .submit. 编辑:您还应该将.clcik更改为.submit。 That's probably the behavior you're looking for. 这可能就是您要寻找的行为。

Your javascript would look like this: 您的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