Using Jquery Ajax to retrieve data from Mysql

0 votes

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. Can someone please help me fix this?

Jun 20, 2022 in PHP by Kithuzzz
• 38,000 points
1,007 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

For retrieving data using Ajax + jQuery, you should 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",""); 

For displaying the data from database, you should 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>";
?>

I hope this helps.

answered Jun 21, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,645 views
0 votes
0 answers

How to retrieve data from multiple tables using a PHP form?

I want to retrieve data from multiple ...READ MORE

Jun 25, 2022 in PHP by narikkadan
• 63,600 points
2,144 views
0 votes
0 answers

How can I store and retrieve images from a MySQL database using PHP?

How can I insert an image in ...READ MORE

Jun 14, 2022 in PHP by narikkadan
• 63,600 points
585 views
0 votes
0 answers

How to retrieve images from MySQL database and display in an html tag

I created a table using the  BLOB column ...READ MORE

Jun 19, 2022 in PHP by narikkadan
• 63,600 points
1,558 views
0 votes
0 answers

How to filter data from a MySQL Database Table with PHP

I'm attempting to code a search box ...READ MORE

Jul 22, 2022 in PHP by narikkadan
• 63,600 points
6,941 views
0 votes
0 answers

How to fetch specific data from MySQL database to my PHP table?

I want to get data from the ...READ MORE

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
3,655 views
0 votes
0 answers

Edit PHP query code depending on image map clicked area using AJAX

I am working on an image map ...READ MORE

Jun 3, 2022 in PHP by Kichu
• 19,040 points
508 views
0 votes
1 answer

Using Jquery Ajax to retrieve data from Mysql

You forgot the table tag Write echo"<table>" before ...READ MORE

answered Dec 30, 2022 in Web Development by anonymous

edited Mar 5 23,608 views
0 votes
0 answers

How to run a PHP function due to a button click (with button 'data' passed)?

I manage some sortiment items on a ...READ MORE

May 28, 2022 in PHP by Kichu
• 19,040 points
4,091 views
0 votes
1 answer

Convert Excel's "41014" date to actual date in PHP or JavaScript

PHPExcel Date handling code: public static function ExcelToPHP($dateValue ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
4,792 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP