Jump to content

Auto-populate form information??


Skipjackrick

Recommended Posts

I have a DB table that contains Contestant ID numbers, and their name.

 

When the contestant comes to Check-in I would like to enter the Contestant ID number and have the HTML form automatically populate a text box with the name of the contestant.

 

Is this possible? 

 

Or do you know of a tutorial or subject that might cover this?

Link to comment
Share on other sites

If you don't care about the page reloading, don't worry about AJAX.

 

<?php
  if($_GET['id']){
    //Lookup info and store it into $c

  }
?>
<html>
  <body>
    <form method="GET" action="">
      Lookup By Contestant ID: <input type="text name="id" /> <input type="submit" value="Lookup" /><br />
    </form>
    <hr>
    <form method="POST" action="submit_page.php">
      Name: <input type="name" value="<?php echo htmlspecialchars($c['name']); ?>" />
etc
    </form>
  </body>
</html>

Link to comment
Share on other sites

If you don't care about the page reloading, don't worry about AJAX.

 

I don't want the page to reload, that will use up precious seconds. 

 

Basically, the contestant walks up to the table and tells me their ID number.  I key in the 3 digit ID into the form.  Information such as their name, what division they are entered in, age and location will auto-fill the form.  This info would be queried from the DB.  This way I can ask the person if their name is "John Smith" after they tell me the contestant ID number.

 

Next, I would enter in the remainder of the necessary information into the form and hit submit.  All of this data would be stored in an additional table on the DB.  Refreshing the page or having an additional page would take too much time.  I'll be entering data for over 600 contestants in less than an hour.

 

Is AJAX my only hope?

 

Link to comment
Share on other sites

If you don't care about the page reloading, don't worry about AJAX.

 

Wait, by reloading do you mean it will fill the info into the form after I click the submit button and the page will be re-displayed with the form fields populated?

 

Or do you mean, once I enter the ID number and move the mouse, or hit tab to the next field on the form, the page would reload automatically with the info from the DB populated in the appropriate form fields?

Link to comment
Share on other sites

I am having difficulty with the function:

 

$_GET

 

I tried to read some of the tutorials online but they are confusing.  I am using the previous code but the form isn't populating once I press lookup.

 

How does the $_GET function know what table to choose in the DB?  The table that contains the data is called "user_info"

 

Here is what I've got.

 

<?php
//Connect to MySQL
$link = mysql_connect("localhost", "user", "pass") or
die ("Check your server connection, could not connect to database");

//make sure we're using the right database
mysql_select_db ("namelookup");

  if($_GET['user_id']){
    //Lookup info and store it into $c

  }
?>
<html>
  <body>
    <form method="GET" action="">
      Lookup By Contestant ID: <input type="text name="id" /> <input type="submit" value="Lookup" /><br />
    </form>
    <hr>
    <form method="POST" action="submit_page.php">
      Name: <input type="name" value="<?php echo htmlspecialchars($c['name']); ?>" />
etc
    </form>
  </body>
</html>

Link to comment
Share on other sites

should be able to....list all the tables/fields the needed data is in to populate the form

 

When you say list?  Is there some syntax for just listing the table needed?  Like this?

 

<?php
$sql = "SHOW TABLES FROM namelookup";
$result = mysql_query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
?>

 

I am setting this up for a fishing competition.  We will have the names of the participants before the event starts.  Once they arrive, we will populate the table "check-in" with the appropriate data.  I want the form to populate the name of the contestant by using the "user_id".  Each contestant was given a user id number to speed up the check-in process.

 

DB = namelookup

 

DB contains two tables

 

"user_info"  and "check_in"

 

 

"user_info" has two fields

user_id

name

 

"check_in" has 7 fields

entry_id = Primary key (auto increment)

user_id

name

time

division

tshirt

age

 

Link to comment
Share on other sites

try this on for size:

 

<?php
//Connect to MySQL
$link = mysql_connect("localhost", "user", "pass") or
   die ("Check your server connection, could not connect to database");

//make sure we're using the right database
mysql_select_db ("namelookup");

if($_GET['id']){
  $error = null;
  if($result = mysql_query(sprintf("SELECT * FROM user_info WHERE user_id = '%s' LIMIT 1",mysql_real_escape_string($_GET['id'])))){
    $c = mysql_fetch_assoc($result);
    if(!$c){
      $error = "User not found";
    }
  }else{
    $error = "Query Failed";
  }
}
?>
<html>
  <body>
    <form method="GET" action="">
      Lookup By Contestant ID: <input type="text" name="id" /> <input type="submit" value="Lookup" /><br />
<?php if($error) echo '<p style="color:red;">Error: '.$error.'</p>'; ?>
    </form>
    <hr>
    <form method="POST" action="submit_page.php">
      Name: <input type="name" value="<?php echo htmlspecialchars($c['name']); ?>" />

...rest of form here...

    </form>
  </body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.