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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Form Questions /

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

Auto Populate Contact Form on Load

Go to End

8 Posts   5896 Views


Community Member, 607 Posts

12 January 2009 at 12:08pm

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.



Community Member, 42 Posts

16 January 2009 at 10:35pm

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.



Forum Moderator, 5523 Posts

18 January 2009 at 10:08pm

Edited: 18/01/2009 10:09pm

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']));


Community Member, 199 Posts

2 March 2009 at 7:46am

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.


Community Member, 607 Posts

2 March 2009 at 7:51am

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.



Community Member, 607 Posts

6 March 2009 at 11:12am

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 != '') {
				// 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) {
				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

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.



Community Member, 199 Posts

6 March 2009 at 9:38pm

Hey Aaron,

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


Community Member, 151 Posts

2 December 2011 at 2:43am

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..


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