I've got a form where a user enters their zip code, and I want to send the form to whichever employee covers that zip. The standard Conditional Recipient select list doesn't really work here because there are way too many zips for one list, and also because when a single zip is covered by more than one person, I want to randomly select which person gets the lead.

So, what I was thinking is to do a bunch of PHP processing in the "Additional Validation" section based on zip (which is $form_values['submitted'][2] in my form), then re-set the hidden email field to be the appropriate address. I figured that then the "validation" would complete and the form would be processed like normal and would send off to the new address.

$zip = $form_values['submitted'][2];

// do a bunch of stuff based on zip code, then eventually get to this: 
		if (in_array($zip, $zip_code_list)){
			$form_values['submitted'][4] = $new_email_address;
			$form_values['submitted_tree']['email_address_send'] =$new_email_address;
			break;
		}

The processing works perfectly, and if I do a print_r($form_values) before and after this code, the email field shows having the correct address in place after the processing. But when I go and look directly at the submissions, all of the entries show the default value in the email field.

Is there some other value I should be changing, or somewhere else I can put this code that will make it change the value of that field (so that the email will get sent to the right person)?

Thanks!

Comments

leenwebb’s picture

After doing some reading about the Forms API, it looks like my code isn't working because $form_values is just something passed into the function that evaluates this code. So I'm changing $form_values, but the rest of the module doesn't care because that variable doesn't get used outside the function.

So! Any thoughts on what variable(s) I should change, from inside the "Additional Validation" box, so that I can make the email recipient based on the zip code field?

Thanks!

quicksketch’s picture

Yes, if you need to actually change the $form_values, you have to use the additional processing textarea instead of the additional validation.

leenwebb’s picture

OH! I thought the additional processing happened *after* the submission had been saved, the emails sent off, etc. Thanks for the help.

I moved my code into the Additional Processing, and it saves all the correct data to the database (yeah!) but saves all the nid's as 0 (boo!). I don't change any values but the email address, but obviously I am managing to break something anyway. Any ideas?

leenwebb’s picture

Status: Active » Closed (fixed)

Ah, that nid problem was my own fault. Typo in my code.

So moving the code into the correct "Additional Processing" field works perfectly. Thanks for your help!