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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved] Contact form not working properly inside template loop


Go to End


2 Posts   1507 Views

Avatar
Anytech

Community Member, 2 Posts

3 March 2014 at 5:10pm

Hello,

I am having trouble getting this thing working. I have been trying for hours!

The site is a parallax site http://www.anytech.co.nz/fdc

For some reason I cannot get this to work. I have the contact from working on other sites. It posts to /fdc/home/ContactForm which does not exist and I never receive any emails.

Any help is appreciated. Thanks.

Page.php (just the bottom bit)

class Page_Controller extends ContentController {
	
	
	public static $allowed_actions = array(
        'ContactForm'
    );
	
    function ContactForm() {
        // Create fields          
        $fields = new FieldList(
			new TextField('nameContact','Name','Name'),
			new TextField('urlContact','Website','Website'),
            new EmailField('emailContact','Email','Email'),
            new TextareaField('obsContact','Message','Message')
        );    
        // Create action
        $actions = new FieldList(new FormAction('SendContactForm', 'Send'));         
        // Create Validators
        //$validator = new RequiredFields();
        //return new Form($this, 'ContactForm', $fields, $actions);
		$form = new Form($this, 'ContactForm', $fields, $actions); 
      	$form->forAjaxTemplate(); 
      	return $form; 
    }
	   
	
	function SendContactForm($data, $form) {
   		Debug::dump($this->requestParams);
		 
        //Set data
        $From = $data['emailContact'];
        $To = 'omikaman@gmail.com';
        $Subject = "Website Contact message";     
        $email = new Email($From, $To, $Subject);
        //set template
        $email->setTemplate('ContactEmail');
        //populate template
        $email->populateTemplate($data);
        //send mail
        $email->send();
		Page_Controller::redirect($this->Link("?success=1"));
    }
}

Page.ss

<% loop getPages() %>	
	<div class="slide" id="slide$Pos" data-slide="$Pos" <% if $ParallaxRatio %>data-stellar-background-ratio="$ParallaxRatio"<% end_if %>>
				<div class="container clearfix">
				<% loop getGridContent() %>
					<div class="grid_$Columns alpha omega">
						$Content
					</div>
				<% end_loop %>
				<% loop $MenuCategorys %>
					<% if $PreColumns > 0 %>
					<div class="grid_$PreColumns alpha omega">
					</div>
					<% end_if %>
					<div class="grid_$Columns alpha omega">
								<h2>$Category</h2>	
								<ul>
								<% loop $MenuItems %>
									<li>$ItemName <br /><% if $Description %>$Description<% end_if %>$Price<br /><br /></li>
								<% end_loop %>	
								</ul>
					</div>	 	
				<% end_loop %>
				<% loop getCP() %>
					<% loop $Up.Up.ContactForm %> 
		            <form name="contactForm" $FormAttributes>             
                    <h2>Contact Form</h2>
		            	<div class="inputsrow clearfix">
			            	<div class="inputicon">
			            		<span></span>
                                $Fields.FieldByName(nameContact)
			              	</div>
			            	<div class="inputicon icon2">
			            		<span></span>
                                $Fields.FieldByName(emailContact)
			            	</div>
			            	<div class="inputicon icon3">
			            		<span></span>
                                $Fields.FieldByName(urlContact)
			            	</div>
			            </div>
			            <div class="commenttext">
                        	$Fields.FieldByName(obsContact)
			            </div>
                      
                    <input type="hidden" name="SecurityID" value="$SecurityID" class="hidden" id="Form_ContactForm_SecurityID" />
       
			            <input type="submit" class="btn5" id="contactSmbt" />
			            <div id="msgContact"></div>
			            <div id="contactLoader" class="loader">
			            	<span></span>
			            </div>
		            </form>
                    <% end_loop %>
				<% end_loop %>
				
		</div>
	</div>
	
<% end_loop %>

Avatar
Anytech

Community Member, 2 Posts

3 March 2014 at 8:50pm

It looks like the code was correct but it needed flushing a few times!
Let me know if anyone needs to see more of the code to solve their problem.