By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

auto fill remaining fields from the database

P: 5
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:
Expand|Select|Wrap|Line Numbers
  1. $qry=mysql_query("SELECT * from user where username =`$_POST['username']`");
  2. $row=mysql_fetch_assoc('$qry);
  3. echo $row['posts] // and so on..
Here is my code so far: i am very new to AJAX.

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
  3.  
  4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  5.  
  6. <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
  7.  
  8. </head>
  9.  
  10. <form action="<?php echo $PHP_SELF;?>"  method="POST">
  11. <fieldset>
  12. <legend>Form</legend>
  13. <label for="username">Username: </label>
  14.  
  15. <input type="text" id="username"  name="username" /> 
  16. <label for="posts">Posts: </label>
  17. <input type="text" id="posts" name="posts"  size="20"/>
  18. <label for="joindate">Joindate: </label>
  19. <input type="text" id="joindate" name="joindate"  size="20"/>
  20.  
  21.  
  22.  
  23. <p><input type="submit" name="submitBtn" value="Submit" /></p>
  24.  
  25. </fieldset>
  26. </form>
  27.  
  28. <script type="javascript/text>
  29. var name = $('.username').val();
  30. $.ajax({
  31.   method: "GET",
  32.   url: "http://website.com/test/autofill.php",
  33.   dataType: 'json',
  34.   data: {
  35.     username: username
  36.   }).success(function( responseObject ) {
  37.     // assuming you have an array of stuff like name, id, email, etc.
  38.     // now i populate another field from the ajax response
  39.     $('.posts').val( responseObject.posts );
  40.   });
  41. </script>
Expand|Select|Wrap|Line Numbers
  1. //connect to database
  2. $name = $_GET['username'];
  3. $return = mysql_query("SELECT * FROM user WHERE username = '$name' LIMIT 1");
  4. $rows = mysql_fetch_array($return);
  5. $formattedData = json_encode($rows);
  6. print $formattedData;
Jan 26 '12 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
Expand|Select|Wrap|Line Numbers
  1. $qry=mysql_query("SELECT * from user where username =`$_POST['username']`");
that query wonít work. a) itís susceptible to SQL injections, b) wrong quotes
Jan 26 '12 #2

P: 5
@Dormilich
I ll work on the injections part later. I am trying to get it worked first:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $name = $_GET['username'];
  3. $return = mysql_query("SELECT posts FROM user WHERE username = '$name' LIMIT 1");
  4. $name = $_GET['username'];
  5. $return = mysql_query("SELECT posts FROM user WHERE username = '$name' LIMIT 1");
  6. $rows = mysql_fetch_array($return);
  7. $formattedData = json_encode($rows);
  8. print $formattedData;
  9. ?>
its still not working.
Jan 26 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
and what does "not working" mean?
Jan 27 '12 #4

P: 5
when i click on the fetch button the page just refreshes instead of fetching the other field. here is my updated code
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
  3.  
  4. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  5.  
  6. <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
  7.  
  8. </head>
  9.  
  10. <form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="POST">
  11. <fieldset>
  12. <legend>Form</legend>
  13. <label for="username">Username: </label>
  14.  
  15. <input type="text" id="username"  name="username" /> 
  16. <button onclick="myrequest()">fetch</button>
  17. <label for="posts">Posts: </label>
  18. <input type="text" id="posts" name="posts"  size="20"/>
  19. <label for="joindate">Joindate: </label>
  20. <input type="text" id="joindate" name="joindate"  size="20"/>
  21.  
  22.  
  23.  
  24. <p><input type="submit" name="submitBtn" value="Submit" /></p>
  25.  
  26. </fieldset>
  27. </form>
  28. <script type="javascript/text>
  29. function myrequest() {
  30. var name = $('.username').val();
  31. $.ajax({
  32.   method: "GET",
  33.   url: "http://url.com/test/autofill.php",
  34.   dataType: 'json',
  35.   data: {
  36.     username: username
  37.   }).success(function( responseObject ) {
  38.     // assuming you have an array of stuff like name, id, email, etc.
  39.     // now i populate another field from the ajax response
  40.     $('.posts').val( responseObject.posts );
  41.   });
  42. }     
  43. </script>
Jan 27 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
a <button>’s type defaults to submit
Jan 27 '12 #6

P: 5
What would you suggest to find a way around it?
Jan 27 '12 #7

Dormilich
Expert Mod 5K+
P: 8,639
there is no need to use a way around it, just straightforwardly define a type attribute yourself.
Jan 27 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.