364,084 Members | 5367 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

auto fill remaining fields from the database

ajaxbegins
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: 6,604
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

ajaxbegins
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: 6,604
and what does "not working" mean?
Jan 27 '12 #4

ajaxbegins
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: 6,604
a <button>’s type defaults to submit
Jan 27 '12 #6

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

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

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML