Forums

Auto Fill Fields from Other Fields

ksaylor 21 Feb, 2012
Okay, I apologize if this has been answered, but I haven't found the answer. Of course, I could just not know the correct search terms. Here's what I want to do: person enters first name and last name, and a field that is for username gets auto-populated with first.last. The person can change the username, but they can just hit submit and the auto-filled username will be used. I'd also like to have a radio button select that changes the type of information that is asked via the form, but I at least need the ability to auto-fill the fields. Thanks in advance for any support :-)
GreyHead 21 Feb, 2012
Hi ksaylor,

Which version of ChronoForms are you using? You can find the version from Site Admin | Extensions | Install/Uninstall | Components in Joomla! 1.5 or Site Admin | Extensions | Extension Manager | Manage in Joomla! 1.6/1.7/2.5.

Bob
ksaylor 22 Feb, 2012
Sorry for the delay, but I was traveling yesterday. I have version 3.2.
GreyHead 22 Feb, 2012
Hi ksaylor ,

In CFv3 you can edit the Form HTML to add this code at the beginning
<?php
$user =& JFactory::getUser();
?>
Then where you need to display the values you can put
<?php echo $user->id; ?>

<?php echo $user->name; ?>
<?php echo $user->email; ?>
<?php echo $user->username; ?>


Bob
ksaylor 24 Feb, 2012
Bob,

Thanks for the reply. I've done what you've said, but I'm not getting the result that I want. Most likely I am just doing something wrong here. I want the user_name field to be populated once the text is entered in the first_name and last_name input fields. Here's the code from my test form:


<?php
$user =& JFactory::getUser();
?>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">First Name</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="" id="text_0" name="first_name" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Last Name</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="" id="text_1" name="last_name" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">

    <label class="cf_label" style="width: 150px;">User Name</label>
    <input class="cf_inputbox required" maxlength="150" size="30" title="" id="text_2" name="user_name" type="text" text="<?php echo $user->first_name; ?>"/>
  </div>
  <div class="cfclear"> </div>
</div>


Thanks again for your help!

-Kase
GreyHead 25 Feb, 2012
Hi ksaylor,

I'm sorry, I completely misunderstood your question. You need to use JavaScript to do this:
window.addEvent('domready', function() {
  $('first_name').addEvent('keyup', username);
  $('last_name').addEvent('keyup', username);
  function username() {
    $('user_name').value = $('first_name').value + '_' + $('last_name').value;
  }
});

Your inputs will need to have ids set.

You may need to add to this to remove any invalid characters from the username (spaces, apostrophes, etc).

Bob
ksaylor 27 Feb, 2012
Thanks for your help! There was one issue with the code you posted (there was a missing '}'), but other than that, it worked perfectly! Here's: the code I ended up using:

window.addEvent('domready', function() {
  $('fname').addEvent('keyup', username);
  $('lname').addEvent('keyup', username);
  function username() {
   $('uname').value = $('fname').value + '.' + $('lname').value;
}});
GreyHead 27 Feb, 2012
Hi ksaylor,

Sorry about the missing } - I fixed my original post for other readers.

Bob
This topic is locked and no more replies can be posted.