Follow Me

Jquery data display from database

Wed, Jun 2, 2010

JQuery, PHP

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 :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • Leave a Reply