ChronoForms 3.1 for Joomla! site Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a "dynamic" subject line using info from the form

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.

Getting ready

We'll use the same form and alter the Admin Email Setup to include the value of the name field.

How to do it...

  1. 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.
  2. Put subject (no quotes, no brackets) into the Dynamic Subject box, Enable the Email Setup, and Apply the changes in the Email Properties box.
  3. 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.

How it works...

This is a fundamental building block in working with ChronoForms — get a result from the form, alter it, and add it back into the results array so that the next step in the form processing will use the altered value.