Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

How to include a class field value in from template?


Reply


3 Posts   647 Views

Avatar
sajok

Community Member, 82 Posts

10 January 2013 at 1:48am

Edited: 10/01/2013 1:51am

Hello,

I have a langing page with a form that uses in it's action attribute a form handler url from an external API. The API takes the form fields values and redirects the user on another "Thank you" page on the same site.

I added a new text field to the LandingPageForm.php class to store the value of the "form handler url". I'm trying to call it from the form template in action attribute with <% control LandingPageForm %>$FormHandlerURL<% end_control %>, but this didn't show any value. This is the code I have:

LandingPageForm.php

class LandingPageForm extends Page
{
   static $db = array(
      'FormHandlerURL' => 'Text' // the form handler URL
   );

   function getCMSFields()
   {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab("Root.Content.Main", new TextField('FormHandlerURL', 'Form handler URL'));
      
      return $fields;   
   }

}

class LandingPageForm_Controller extends Page_Controller
{   
   static $allowed_actions = array(
      'LandingPageForm'
   );
   
   //The function which generates our form
   function LandingPageForm()
   {
   // Create fields
    $fields = new FieldSet(
       new TextField('FirstName', 'First Name'),
       new TextField('LastName', 'Last Name'),
       new EmailField('Email', 'Email')
      );
       
    // Create action
    $actions = new FieldSet(
       new FormAction('SendLandingPageForm', 'Send me an intro package')
    );
      
      // Create action
      $validator = new RequiredFields('FirstName', 'LastName', 'Email');
   
    $form = new Form($this, 'LandingPageForm', $fields, $actions, $validator);
    $form->setTemplate('LandingForm');
    return $form;
   }
   
   function SendLandingPageForm($data, $form) {
       ....
   }

}

landingForm.ss

<form id="Form_LandingPageForm" action="<% control LandingPageForm %>$FormHandlerURL<% end_control %>" method="post" enctype="application/x-www-form-urlencoded">

   <% if Message %>
   <p id="{$FormName}_error" class="message $MessageType">$Message</p>
   <% else %>
   <p id="{$FormName}_error" class="message $MessageType" style="display: none;"></p>
   <% end_if %>

<fieldset>
         <label class="left" for="Form_LandingPageForm_FirstName">First Name</label>
         <input type="text" class="text" id="Form_LandingPageForm_FirstName" name="firstname" placeholder="First Name *" value="">
      
         <label class="left" for="Form_LandingPageForm_LastName">Last Name</label>
         <input type="text" class="text" id="Form_LandingPageForm_LastName" name="lastname" placeholder="Last Name *" value="">
      
         <label class="left" for="Form_LandingPageForm_Email">Email</label>
         <input type="text" class="text" id="Form_LandingPageForm_Email" placeholder="Email *" name="email" value="">
      
         <input class="hidden nolabel" type="hidden" id="Form_LandingPageForm_SecurityID" name="SecurityID" value="911284708">
      
      <div class="clear"><!-- --></div>
   </fieldset>
   
   <% if Actions %>
   <div class="Actions">
      <% control Actions %>
         $Field
      <% end_control %>
   </div>
   <% end_if %>
   
   <p class="notice">We will never share your private information.</p>
   
</form>

I appreciate you help, thanks.

sajok

Avatar
Willr

Forum Moderator, 5513 Posts

10 January 2013 at 5:43pm

Try <% control Controller %>$FormHandlerURL<% end_control %> as that will change your scope to the controller (where your FormHandlerURL variable is.

Avatar
sajok

Community Member, 82 Posts

27 January 2013 at 7:47am

Thanks it's working now..