
Here's one more "hijacking" example to end this chapter. ChronoForms gives you two choices for your e-mail subject — either a fixed phrase using the "subject" element in the Email Setup, or a value returned from the form using the "dynamic subject" element.
It's a frequent request to have a subject line that's a bit of each — a fixed text with a variable element. We can do this with another code snippet in the OnSubmit Before box.
We'll use the same form and alter the Admin Email Setup to include the value of the name field.
- In the Form Editor go to the Email Setup by clicking on the Setup Emails tab and find the setup for the e-mail to the site administrator, delete the Subject element and drag in a Dynamic Subject instead.
- Put
subject
(no quotes, no brackets) into the Dynamic Subject box, Enable the Email Setup, and Apply the changes in the Email Properties box. - Go to the Form Code tab and open the OnSubmit Before email box. Add this snippet to any code that's already there:
<?php $name = JRequest::getString('text_1', '' ,'post'); $subject = "New subscription from $name"; JRequest::setVar('subject', $subject); ?>
Line by line:
$name = JRequest::getString('text_1', '' ,'post')
;This gets the value of the name input from the form results (remember the input name was
'text_0'
) and assigns it to the $name variable.$subject = "New subscription from $name"
;This inserts the value of $name into the string, so the value of $subject might be New subscription from Jenny Smith
JRequest::setVar('subject', $subject)
;This takes out new subject line and saves it into the
$_POST
array where the ChronoForms e-mailer can find it.