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.
Please make sure your
$row[1] , $row[2]
contains correct value, we do assume here that1 = 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:
In your HTML
A little note:
Try learing PDO http://php.net/manual/en/class.pdo.php since
mysql_* functions
are no longer encouraged..For retrieving data using Ajax+jQuery, you must write the following code:
For mysqli connection, write this:
For displaying the data from database, you must write this :
You can't return ajax return value. You stored global variable store your return values after return.
Or Change ur code like this one.
needs to be: