Auto populate forms

tknospdr - May 27, 2008 - 01:45

Okay, I'm working on a site where the users have to fill out several fairly long forms. Is there any way to have overlapping info auto filled in on subsequent forms once it's been done on the first form?

Thanks,
David

If this isn't possible could

tknospdr - May 27, 2008 - 14:56

If this isn't possible could it be done from info in the user's profile? IE if I set up custom fields in the profile such as 'Legal name' and 'Address' could those be brought into forms on nodes the users are filling out?

Thanks,
David
http://www.floridapets.org

Wow, nothing

tknospdr - May 28, 2008 - 18:18

Wow, nothing eh?

Thanks,
David
http://www.floridapets.org

I can't believe that this is

tknospdr - May 29, 2008 - 19:33

I can't believe that this is something that hasn't been solved and posted before. I searched, but didn't see anything.

Thanks,
David
http://www.floridapets.org

I've done stuff like this,

CleanCutRogue - May 30, 2008 - 03:25

I've done stuff like this, as well as adding some javascript to a checkbox for "shipping same as billing" to auto-populate one set of fields from another. It can't be done in core drupal, you have to theme your input form, then you can put values in fields when they're rendered, derived from user values or whatnot.

If you've never themed an input form, there're a few steps to doing it (it's not simple, but not overly complex either). You have to make a special phptemplate function call from your theme's template.php, like this:

if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'somecontenttype'){
  function phptemplate_node_form($form) {return _phptemplate_callback('somecontenttype-edit', array('user' => $user, 'form' => $form));}
}

if (arg(0) == 'node' && arg(2) == 'edit'){
  $node = node_load(arg(1));
  if ($node->type=='somecontenttype') {
    function phptemplate_node_form($form) {return _phptemplate_callback('somecontenttype-edit', array('user' => $user, 'form' => $form, 'node' => node_load(arg(1))));}
  }
}

This will allow the phptemplate engine to use somecontenttype-edit.tpl.php to craft the edit form. So the next step is to create a file called somecontenttype-edit.tpl.php in the same directory as your template.php file and put the following code in it:

<?php
   
print drupal_render($form);
?>

If you had a simple text CCK field in your edit form called "companyname" (and therefore it was called $node->field_companyname[0]['value'] in code), you could pre-populate it from the current user's profile field of the same name by doing something similar to this in somecontenttype-edit.tpl.php instead:

<?php
 
global $user;
 
$user = user_load(array('uid'=>$user->uid));
 
$form['field_companyname'][0]['value'] = $user->companyname;

  print
drupal_render($form);
?>

This would pre-populate the contents of that form field prior to rendering it to the user's screen. The only down-side to doing it this way is that the input form's functionality is now tied to your theme, and changing themes at a later date will require you to be smart about it.

Wow, thanks for the info. I

tknospdr - May 31, 2008 - 11:30

Wow, thanks for the info. I think that's a little out of my range for now. I was afraid that it was going to involve all that scary PHP stuff.

What a perfect opportunity for someone to build this into a module... someone way smarter than I am that is.

Thanks,
David
http://www.floridapets.org

CleanCutRogue, This is

iansears - October 23, 2008 - 02:45

CleanCutRogue,

This is fantastic and just what I've been hunting for, well, very close to it.
http://drupal.org/node/306147

Ok, now you described that you placed a checkbox "Shipping same as Billing". Does this mean that checking hat box is the event triger for an onChange JQuery or something that THEN auto-populates your address fields? Do please explain if you have time.

This will save me days of hand-coding I think, and your theme-tied caveat is fine with me.

Thanks in advance,
Ian Sears

 
 

Drupal is a registered trademark of Dries Buytaert.