Jquery data display from database

Hi Guys,

I am posting simple example of fetching data from database using jquery. This code display password stored in database using jquery instantly. You just need to call simple jquery function and it will return password as a response text.

First, you will create index.php in which you will copy this code. Set database connections on the top.

<?php
	mysql_connect("localhost","root","");
	mysql_select_db("jquery_test");
	$result1=mysql_query("select * from users_tb where id=1");
	$row=mysql_fetch_array($result1);
	$id=$row["id"];
?>

Include jquery file. You can download it from here.

<script language="javascript" src="jquery-1.2.6.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function displayPassword(id)
{
	var aurl='show_password.php?id='+id;
	var result = $.ajax({
	type: "GET",
	url: aurl,
	data: "stuff=1",
	async: false
	}).responseText;
	if(result)
	{
		$("#password_td").html(result);
	}
}
</script>
<table width="100%" cellspacing="3" align="center" class="admin_form">
<tr>
	<td nowrap="nowrap" width="30%" class="admin_form_label">
	<a href="javascript:void(0)" onclick="displayPassword(<?php echo $id; ?>)">Show Password</a></td>
				<td class="admin_form_field" id="password_td"><?php if(isset($_GET['pass']) && $_GET['pass']!="") echo $_GET['pass'];?>
	</td>
</tr>
</table>

Now our basic structure is complete. You need to create one more file. In the jquery function, request has been sent to show_password.php with database id. This file simply echo database user password.

show_password.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("jquery_test");

$result=mysql_query("select * from users_tb where id='".$_GET["id"]."'");
$row=mysql_fetch_array($result);

$password=$row["password"];
echo $password;
?>

More jquery stuff in next posts.

Enjoy it :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>