View Full Version : auto fill fields from the database

01-25-2012, 05:39 PM
I have a simple form right now:

<form action='<? echo $PHP_SELF;?>' method='POST'>
Username:<input type='text' name='username'><br>
Email:<input type='text' name='email'><br>
Posts:<input type='text' name='posts'><br>
Joindate<input type='text' name='join'><br>
<input type="submit" value="Submit" />

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:
$qry=mysql_query("SELECT * from user where username =`$_POST['username']`");
echo $row['posts] and so on..

After it is autogenerated he can edit the fields and submit to update the database fields.

Please move this post if I am in the wrong forum.

01-25-2012, 06:24 PM
This cannot be done in PHP. PHP is a server side language, once it is done with its request and sent its response, its job is done and cannot be interacted with without the use of a new request.
This behaviour you are looking for uses javascript to facility an AJAX communication with the PHP page providing the get/post the same way the standard processing occurs in a PHP page. Same rules apply, PHP returns its results and AJAX handles the new data to refresh an existing page in the background.

01-25-2012, 06:27 PM
Thanks.I am new to AJAX and i am trying to find some code that does something similar so that i can learn how to do it and apply that concept to my code.