Using Jquery Ajax to retrieve data from Mysql

2019-01-01 05:25发布

list.php: A simple ajax code that I want to display only records of the Mysql table:

<html>
        <head>
        <script src="jquery-1.9.1.min.js">
        </script>

        <script>
        $(document).ready(function(){
        var response = '';
        $.ajax({ type: "GET",   
                 url: "Records.php",   
                 async: false,
                 success : function(text)
                 {
                     response = text;
                 }
        });

        alert(response);
        });
        </script>
        </head>
        <body>

        <div id="div1"><h2>Let jQuery AJAX Change This Text</h2>  </div>
        <button>Get Records</button>

        </body>
        </html>

Records.php is the file to fetch records from Mysql.
In the Database are only two fields: 'Name', 'Address'.

        <?php
                //database name = "simple_ajax"
                //table name = "users"

                $con = mysql_connect("localhost","root","");
                $dbs = mysql_select_db("simple_ajax",$con);
                $result= mysql_query("select * from users");
                $array = mysql_fetch_row($result);
                ?>
                    <tr>
                    <td>Name: </td>
                    <td>Address: </td>
                    </tr>


                <?php

                while ($row = mysql_fetch_array($result))
                 {

                    echo "<tr>";
                    echo "<td>$row[1]</td>";
                    echo "<td>$row[2]</td>";
                    echo "</tr>";
                }   

                ?> 

This code is not working.

4条回答
皆成旧梦
2楼-- · 2019-01-01 05:54

Please make sure your $row[1] , $row[2] contains correct value, we do assume here that 1 = Name , and 2 here is your Address field ?

Assuming you have correctly fetched your records from your Records.php, You can do something like this:

$(document).ready(function()
{
    $('#getRecords').click(function()
    {
        var response = '';
        $.ajax({ type: 'POST',   
                 url: "Records.php",   
                 async: false,
                 success : function(text){
                               $('#table1').html(text);
                           }
           });
    });

}

In your HTML

<table id="table1"> 
    //Let jQuery AJAX Change This Text  
</table>
<button id='getRecords'>Get Records</button>

A little note:

Try learing PDO http://php.net/manual/en/class.pdo.php since mysql_* functions are no longer encouraged..

查看更多
春风洒进眼中
3楼-- · 2019-01-01 06:00

For retrieving data using Ajax+jQuery, you must write the following code:

 <html>
 <script type="text/javascript" src="jquery-1.3.2.js"> </script>

 <script type="text/javascript">

 $(document).ready(function() {

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

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

    });
});
});

</script>

<body>
<h3 align="center">Manage Student Details</h3>
<table border="1" align="center">
   <tr>
       <td> <input type="button" id="display" value="Display All Data" /> </td>
   </tr>
</table>
<div id="responsecontainer" align="center">

</div>
</body>
</html>

For mysqli connection, write this:

<?php 
$con=mysqli_connect("localhost","root",""); 
if (!$con)
{
    die('Could not connect: ' . mysqli_error());
}   
?>

For displaying the data from database, you must write this :

<?php
include("connection.php");
mysqli_select_db("samples",$con);
$result=mysqli_query("select * from student",$con);

echo "<table border='1' >
<tr>
<td align=center> <b>Roll No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Address</b></td>
<td align=center><b>Stream</b></td></td>
<td align=center><b>Status</b></td>";

while($data = mysqli_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 "<td align=center>$data[4]</td>";
    echo "</tr>";
}
echo "</table>";
?>
查看更多
何处买醉
4楼-- · 2019-01-01 06:12

You can't return ajax return value. You stored global variable store your return values after return.
Or Change ur code like this one.

AjaxGet = function (url) {
    var result = $.ajax({
        type: "POST",
        url: url,
       param: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
       async: false,
        success: function (data) {
            // nothing needed here
      }
    }) .responseText ;
    return  result;
}

查看更多
素衣白纱
5楼-- · 2019-01-01 06:15
$(document).ready(function(){
    var response = '';
    $.ajax({ type: "GET",   
         url: "Records.php",   
         async: false,
         success : function(text)
         {
             response = text;
         }
    });

    alert(response);
});

needs to be:

$(document).ready(function(){

     $.ajax({ type: "GET",   
         url: "Records.php",   
         async: false,
         success : function(text)
         {
             alert(text);
         }
    });

});
查看更多
登录 后发表回答