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 /

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

Ajax form issue

Go to End

2 Posts   2117 Views


Community Member, 23 Posts

22 December 2010 at 6:02pm

Edited: 22/12/2010 6:05pm

I am trying to create an ajax contact form. The form is working fine (ajax and regular POST) as long as the form data validates, but when there are validation errors, I am getting unexpected output when submitting by ajax. The following seems to be returned by my form action method, the output of which, I intend to use as a confirmation message.

validationError('Email', '\"Email*\" is required..', 'required'); validationError('Comments', '\"Comments*\" is required..', 'required'); statusMessage('Validation failed', 'bad'); $(document).ready(function() { jQuery('#nav').superfish({ speed: 'fast', autoArrows: false }); }); (function($) { $(document).ready(function() { $("#Form_Form").submit(function(){ $.post( '/contact-us/Form', $(this).serializeArray(), function(data){ $("#Form_Form").replaceWith(data); } ); return false; }); }); })(jQuery); 

Any idea what's happening here? If I disable javascript validation -- $form->getValidator()->setJavascriptValidationHandler('none'); -- the error goes away, but other issues are introduced. Ideally, I'd like to keep the javascript validation.

Thanks in advance for any help you can give!


Here is my controller and form code (somewhat truncated):

class ContactPage_Controller extends Page_Controller
	function init() {
			(function($) {
				$(document).ready(function() {
						return false;

	function Form() {
		$Params = Director::urlParams();
		$fields = new FieldSet(
			new TextField('Name', 'Name*'),
			new EmailField('Email', 'Email*'),
			new TextareaField('Comments','Comments*')
		$actions = new FieldSet(
			new FormAction('SendContactForm', 'Send')
		$validator = new RequiredFields('Name', 'Email', 'Comments');
		$form = new Form($this, 'Form', $fields, $actions, $validator);
		return $form;
	function SendContactForm($data) {
	 	$From = $data['Email'];
		$To = 'email@address';
		$Subject = "You've got mail";
		$email = new Email($From, $To, $Subject); 
		$success = $email->send();
		if (Director::is_ajax()) {
			if ($success) {
				return 'Success';
			} else {
				return 'Error';
		} else {
			/*non-ajax code*/



Community Member, 23 Posts

5 January 2011 at 4:39am

Anyone have any thoughts on this? I am stuck. (Bump.)