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

Ajax and Forms


Go to End


4 Posts   2332 Views

Avatar
J2-Paul

Community Member, 51 Posts

10 October 2010 at 9:35am

I am starting to look at AJAX and forms. I am having no problem retrieving records and loading forms into the divs and so on. However, I am having trouble figuring out how to submit a form date using AJAX and thus prevent a full page reload.

Any help on how this is achieved in SS would be much appreciated. Some example code event better!

Thanks in advance.

Paul

Avatar
J2-Paul

Community Member, 51 Posts

11 October 2010 at 8:09am

Thanks Martin. I have followed these and all all seem to be working. My problem is with this part of the guide from the form:recipes.

class RegistrationForm_Controller extends Page_Controller {
    function doform($data, $form) {
        // your form actions
        FormResponse::update_dom_id('myForm', '<form>myformcontent</form>');
        FormResponse::status_message('Done', 'good');
 
        // will automatically show the status-message if called by ajax, or redirect on a normal HTTP-request
        return FormResponse::respond()
    }
}

I note the line "will automatically show the status-message if called by ajax, or redirect on a normal HTTP-request"

I can not get my form to do anything other than the normal HTTP-request post submit.

Here are the relevant code snippets. I am sure I am missing something obvious, I should maybe point out that I am new to the AJAX world! (if that was not obvious already!).

Any help much appreciated.

Thanks in advance.

The form:

	function Studentedit() { 
	 
		$goeditid=Session::get('goeditid');
//		debug::show($goeditid);
		$student = DataObject::get_by_id('Student',$goeditid); 		

//		$student = DataObject::get_by_id('Student',$_GET['id']); 		
		$actions = new FieldSet( 
			new FormAction('doUpdateStudent','Save')
			); 

		if ($student) {
			$form= new StudentForm2($this,'StudentEdit',$actions);
			$form->loadDataFrom($student);
			return $form;
		}
		else {
			return $this->httpError(404,'Sorry, student does not exist'); 

		}

	}

The submit method:


	function doUpdateStudent($data,$form) {

    $student = DataObject::get_by_id('Student',$data['ID']); 
     
	if ($student) { 
		$form->saveInto($student);
		$student->write();       
	}
	
	if($this->isAjax) {
		FormResponse::update_dom_id('ajaxEditForm', '<p>DONE</p>'); 
		FormResponse::status_message('Done', 'good'); 
	}
	else {
		return Array();
	}
 	
        return FormResponse::respond();
    } 

Avatar
J2-Paul

Community Member, 51 Posts

14 October 2010 at 6:33am

Can anyone help here. Not looking for the crown jewels...just a little nudge in the right direction.

Thanks in advance for any assistance.

Paul