auto fill remaining fields from the database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajaxbegins
    New Member
    • Jan 2012
    • 5

    auto fill remaining fields from the database

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

    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;
    Last edited by Dormilich; Jan 26 '12, 10:13 PM. Reason: added more [CODE] [/CODE] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Code:
    $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

    Comment

    • ajaxbegins
      New Member
      • Jan 2012
      • 5

      #3
      Originally posted by Dormilich
      that query won’t work. a) it’s susceptible to SQL injections, b) wrong quotes
      I ll work on the injections part later. I am trying to get it worked first:

      Code:
      <?
      $name = $_GET['username'];
      $return = mysql_query("SELECT posts FROM user WHERE username = '$name' LIMIT 1");
      $name = $_GET['username'];
      $return = mysql_query("SELECT posts FROM user WHERE username = '$name' LIMIT 1");
      $rows = mysql_fetch_array($return);
      $formattedData = json_encode($rows);
      print $formattedData;
      ?>
      its still not working.
      Last edited by Dormilich; Jan 27 '12, 06:22 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        and what does "not working" mean?

        Comment

        • ajaxbegins
          New Member
          • Jan 2012
          • 5

          #5
          when i click on the fetch button the page just refreshes instead of fetching the other field. here is my updated code
          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 $_SERVER['PHP_SELF']; ?>"  method="POST">
          <fieldset>
          <legend>Form</legend>
          <label for="username">Username: </label>
          
          <input type="text" id="username"  name="username" /> 
          <button onclick="myrequest()">fetch</button>
          <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>
          function myrequest() {
          var name = $('.username').val();
          $.ajax({
            method: "GET",
            url: "http://url.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>

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            a <button>’s type defaults to submit

            Comment

            • ajaxbegins
              New Member
              • Jan 2012
              • 5

              #7
              What would you suggest to find a way around it?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                there is no need to use a way around it, just straightforward ly define a type attribute yourself.

                Comment

                Working...