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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

Auto Populate Fields


Go to End


2349 Views

Avatar
VictorH

Community Member, 29 Posts

20 October 2011 at 5:26am

I have the code below that is displaying a form and when a user begins filling out the "Credit Union*" field it generates a list of suggestion which they can choose from to populate the field. This is extended from the tutorial on this page:
http://www.ssbits.com/tutorials/2011/create-an-ajax-auto-complete-member-search-with-silverstripe-and-jquery/

Everything is working thus far but I want to add some extra functionality. I want to add a hidden field, a city text field and state dropdown field but if the user uses a suggestion from the list I want it to automatically fill out the 3 new fields. Adding the fields is not a problem. I need help populating the fields based on the suggestion selected.

If no suggestion is selected a user enters custom entry for Credit Union then the fields don't automatically populate.

Credit Unions are a dataObject with the related information.

CreditUnion.php

<?php

class CreditUnion extends DataObject {
	static $db = array(
		'Name' => 'Text',
		'CN' => 'Text',
		'City' => 'Text',
		'State' => 'Text'
	);
	
	static $has_one = array(
		'Page' => 'Page'
	);

	function getCMSFields_forPopup() {
		$fields = new FieldSet();
	
		$fields->push (new TextField('Name', 'Name'));
		$fields->push (new TextField('CN', 'CN'));
		$fields->push (new TextField('City', 'City'));
		$fields->push (new TextField('State', 'State'));
		
		return $fields;
	}
}

?>

FormPage.php

<?php
class FormPage extends Page {
	static $db = array('ThankYou' => 'Varchar(255)');
	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		
		$fields->addFieldToTab('Root.Content.Main', new TextareaField('ThankYou', 'Thank You Message'));
				
		return $fields;
	}
}

class MemberSearchPage_Controller extends Page_Controller {
	static $allowed_actions = array(
		'SignUpForm',
		'results',
		'doSignUp'
	);
	
	public function init() {
		parent::init();
		
		//JS
		Requirements::javascript("http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js");
		Requirements::javascript("autocomplete/javascript/jquery.autocomplete-min.js");
		Requirements::CustomScript("
		
			jQuery(document).ready(function() {
				var options, a;
				
				jQuery(function(){
				  options = { serviceUrl:'" . $this->Link('results') . "' };
				  a = jQuery('#Form_SignUpForm_credit_union').autocomplete(options);
				});						
				
			})
		
		");
	}
	
	public function SignUpSuccess() {
		return isset($_REQUEST['signupsuccess']) && $_REQUEST['signupsuccess'] == "1";
	}
	
	public function SignUpForm() {
		// Sign Up Form Fields
		$fields = new FieldSet(
			new TextField('first_name','First Name*'),
			new TextField('last_name','Last Name*'),			
			new TextField('credit_union', 'Credit Union*', $this->getSearchQuery()),
			new EmailField('email','E-mail address*')
		);
		
		// On Submit
		$actions = new FieldSet(
			new FormAction('doSignUp', 'Submit')
		);
		
		// Fields to validate
		$validator = new RequiredFields('first_name', 'last_name', 'credit_union', 'email');

		return new Form($this, 'SignUpForm', $fields, $actions, $validator);
	}

	function results() {
		if($credit_union = $this->getSearchQuery()) {
			$credit_union = Convert::raw2xml($credit_union); 
			
			//Search for our query - Pretty basic example here
			$Results = DataObject::get('CreditUnion', 
				"MATCH (Name) AGAINST ('$credit_union') 
				OR Name LIKE '%$credit_union%'"
			);

			//For AutoComplete
			if(Director::is_ajax()) {
				$Members = $Results->map('ID', 'Name');
				
				$Suggestions = "['" . implode("','",$Members) ."']"	;
				
				return $json = "{
					query : '$credit_union',
					suggestions : $Suggestions
	 			}";
			}
		}
	}
	
	function doSignUp($data, $form) {
		// Store in DB
		$submission = new SignUpSubmission();
        $form->saveInto($submission);
        $submission->write();
		
		// Send Email to Admin
		$From = $data['email'];
		$To = 'email@test.com';
		$Subject = 'Subject';
		$email = new Email($From, $To, $Subject);
		$email->setTemplate('Template');
		$email->populateTemplate($data);
		$email->send();
		
		// Send Email to User
		$autoFrom  = 'email@test.com';
		$autoTo = $data['email'];
		$autoSubject = 'Subject';
		$autoEmail = new Email($autoFrom, $autoTo, $autoSubject);
		$autoEmail->setTemplate('Template');
		$autoEmail->send();

		Director::redirect(Director::baseURL(). $this->URLSegment . "/?signupsuccess=1");
	}

	function getSearchQuery() {
		if($this->request)
			return $this->request->getVar("query");
	}
}