Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Auto Populate Contact Form on Load

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 4130 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Auto Populate Contact Form on Load Link to this post

    Hi all,

    I have a simple contact form (using the 2.3.0 with the userforms extension).

    I am also using ecommerce, but just as a catalogue. (all products are un-buyable).

    What I have is a link to the contact form on each product page, with the products code added to the URL. What I want to do is to automatically populate a field on the form with this code - something that would normally be as easy as value="<?php $_GET['code']; ?> with strait PHP.

    Can someone advise on how to do this? I have made a function in the UserDefinedForm.php file that takes the get param, urldecodes it, and returns the text. But I just can't work out how to get the value populated in that particular field.

    Cheers
    Aaron

  • fishe
    Avatar
    Community Member
    42 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    I have the same issue!

    Would you also be so kind as to paste your code for the function in UserDefinedForm.php that you mention? I think this might help me out.

    Cheers

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Having a look at the code I think you can call populateFromPostData() (this is tad of a hack but anyways I blame lack of documentation on it!). Note I was looking at trunk code but I don't think its changed that much from the stable release.

    // trunk/UserDefinedForm.php line 265

    foreach( $this->Fields() as $field ) {
    $fields->push( $field->getFormField() );
    $fields->populateFromPostData(array('Title' => $field->Title, 'Default' => $_GET['yourcode']));
    ...

  • Briohny
    Avatar
    Community Member
    199 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Hi Double-A-Ron,

    Would you mind posting your code that solved this problem. I would like to do the same thing, only i don't know where to start.

    Many thanks.

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Hi Briohny,

    I actually didn't end up doing this sorry - the project that it was for stalled.

    I think Will's code example will be the best place to start.

    Cheers
    Aaron

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Hi Briohny

    I ended up hacking something together for another client if you are interested. I gave up on doing this using Sapphire directly and went with Javascript instead. Note that this is a severly limited hack, but we only need it for one contact form. Here's what I did:

    1. Add a function to the form controller to get the var from the URL (or post, or cookie)
    /userforms/code/UserDefinedForms - Inside the UserDefinedForm_Controller class

    /**
        * MODADC - Get and decode the selected product for the form autopopulation
        */
       function getProductDetails() {
          if(isset($_GET['product_item'])) {
             return urldecode($_GET['product_item']);
          } else {
             return '';
          }
       }

    2. Use the UserDefinedForm_Controller's init() method to get the var, and inject some JS code onto the page.
    /userforms/code/UserDefinedForms - around line 163 - under the line "Requirements::javascript(THIRDPARTY_DIR . 'jsparty/behaviour.js');"

    // Call the function that gets the GET var
          $product_item = $this->getProductDetails();
          
          // Only inject the javascript if we have the required var
          if($product_item != '') {
          
             Requirements::customScript(<<<JS
                // This function adds functions to the onload event, so all the orignal javascript still works.
                // No changes needed in this function at all
                function addLoadEvent(func) {
                   var oldonload = window.onload;
                   if (typeof window.onload != 'function') {
                      window.onload = func;
                   } else {
                      window.onload = function() {
                         if (oldonload) {
                            oldonload();
                         }
                         func();
                      }
                   }
                }
                
                function setDefaults() {
                   // I used firebug to get the id of the field on the form I want to target
                   // Use firebug to do the same in your case
                   if(document.getElementById('Form_Form_EditableTextField3')) {
                      document.getElementById('Form_Form_EditableTextField3').value = '$product_item';
                   }
                }
                
                // Add the setDefaults function to the onload event, so it is only called after the page loads
                addLoadEvent(setDefaults);
       
    JS
             );
          
          }

    IMORTANT: The above uses heredoc syntax, so that very last 'JS' must be on it's own line with nothing else, and have no indentation. Doesn't look the prettiest, but it allows us to type custom javascript without escaping. If you wanted to tidy it up, put your javascript in a seperate file and include it the same as the two requirements lines already there.

    Hope that helps. Works for me and my client.

    Cheers
    Aaron

  • Briohny
    Avatar
    Community Member
    199 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Hey Aaron,

    Thanks so much for this. I'll give it a whirl!

  • lozhowlett
    Avatar
    Community Member
    146 Posts

    Re: Auto Populate Contact Form on Load Link to this post

    Hi, this doesnt work for 2.4... anyone got a better solution, surely this is really simple stuff that should be easy to do?

    I have this URL..

    candidates/apply-for-a-job/?Form_Form_EditableTextField39=JOB1

    And want to populate the form field... any ideas?

    Thanks!

    4130 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.