Autofill Name and Email fields for forms
What it is
The following sample form will automatically fill with the username and registered email address.
Why Use It
If you have an external form process script that you want your users to submit a form to. (I used it for a custom timesheet form on a company website)
How to use it
For your form's input elements for name and email, add the following two values within and make sure your input type is set to PHP code.
- For the name field, insert
value="<?php
global $user;if($user->uid) { print check_plain($user->name);}?>" - For the email field, insert
value="<?php
global $user;if($user->uid) { print check_plain($user->mail);}?>"
Example Form
For a quick-and-dirty form, paste the code below into a page on your site and make sure your input type is set to PHP code.
<form name="form1" method="post" action="www.site.com">
<fieldset><legend>User Info</legend>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="<?php
global $user;if($user->uid) { print check_plain($user->mail);}?>" ></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="email" value="<?php
global $user;if($user->uid) { print check_plain($user->mail);}?>" ></td>
</tr>
</table>
</fieldset>
</form>