What I need is, when the user fills his username and does one of the following:
1.presses tab
2.Presses enter
3.clicks on a fetch button(fetch button does not exist now,i would like to know how to create it using javascript or anything else that suits my criteria)
Once he does any of the above it should automatically generate the remaining fields from the database.
The query would look like this:
Here is my code so far: i am very new to AJAX.
1.presses tab
2.Presses enter
3.clicks on a fetch button(fetch button does not exist now,i would like to know how to create it using javascript or anything else that suits my criteria)
Once he does any of the above it should automatically generate the remaining fields from the database.
The query would look like this:
Code:
$qry=mysql_query("SELECT * from user where username =`$_POST['username']`");
$row=mysql_fetch_assoc('$qry);
echo $row['posts] // and so on..
Code:
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
</head>
<form action="<?php echo $PHP_SELF;?>" method="POST">
<fieldset>
<legend>Form</legend>
<label for="username">Username: </label>
<input type="text" id="username" name="username" />
<label for="posts">Posts: </label>
<input type="text" id="posts" name="posts" size="20"/>
<label for="joindate">Joindate: </label>
<input type="text" id="joindate" name="joindate" size="20"/>
<p><input type="submit" name="submitBtn" value="Submit" /></p>
</fieldset>
</form>
<script type="javascript/text>
var name = $('.username').val();
$.ajax({
method: "GET",
url: "http://website.com/test/autofill.php",
dataType: 'json',
data: {
username: username
}).success(function( responseObject ) {
// assuming you have an array of stuff like name, id, email, etc.
// now i populate another field from the ajax response
$('.posts').val( responseObject.posts );
});
</script>
Code:
//connect to database
$name = $_GET['username'];
$return = mysql_query("SELECT * FROM user WHERE username = '$name' LIMIT 1");
$rows = mysql_fetch_array($return);
$formattedData = json_encode($rows);
print $formattedData;
Comment