1

I have two fields on a Content Type called Property that requires the same information and I am trying to have it so that it only needs to be inputted one time be the user.

I am using the module Address Field and Locations and need to fill out the zip codes with the same zip code. I need to get it so I only have to input the zip code one time and it fills both. I have tried a hook form alter (my example below) but the second field does not get the text unless I save the node again. I understand why but I am not sure how to go about fixing this.

function zip_to_zip_form_alter(&$form, $form_state, $form_id) {

switch($form_id) {

  case 'property_node_form':

$form['locations'][0]['#default_value']['postal_code'] = $form['field_address']['und'][0]['#address']['postal_code'];


  break;

} }

Suggestions welcome. Thanks!

  • You can do this by hook_node_api. – Sumit Madan Dec 20 '12 at 6:03
0

What you are doing is happening in the browser, so drupal, or anything else on the server, obviously doesn't know what's going on until you hit submit.

So you have to use jQuery that is running there in the client, and one way would be along the lines of:

$('#IDOFYOURFIRSTZIPCODE').live('change', function() {
  $('#IDOFYOURSECONDZIPCODE').val($('#IDOFYOURFIRSTZIPCODE').val());
});  

The #IDs will look something like #edit-field-FIELDNAME-0-value but basically what it is doing is changing the second value to the first when the first changes.

Put that in a module's or theme's js and you should be set. (I suppose you could [should?] add it with a drupal_add_js() right from your form_alter().)

(For those jQuery experts out there, I suppose I should have used on() or delegate() instead of live() but this works on both D6 and D7 with stock jQuery)

  • I just wanted to say thanks! This worked. I needed to do this for Drupal 7 and it was for the admin overlay. I ended up creating a module that hooked into the admin overlay and called the jQuery script. Worked like a charm! – PoeticNinja Jul 11 '12 at 17:25
  • glad I could help! – Jimajamma Jul 11 '12 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.