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

Call ajax to form action


Go to End


1849 Views

Avatar
Craftnet

Community Member, 58 Posts

29 June 2013 at 2:05am

Edited: 29/06/2013 2:07am

Hi, I have form like extend
I add to function

if(Director::is_ajax()){
			return $this -> renderWith("Footer");
		} else {//DO SOMETHING}

How to call in js to this function from Form ("AddObiekt")

When in JS add url: ajax/AddObiekt - he call to Page Controller
I wont that AJAX in JS call to function AddObiekt from class AddObiektForm extends Form

AddObiektForm

<?php
class AddObiektForm extends Form {
		
	function __construct($controller, $name) {
		
		$title = new TextField('Title', 'Tytuł');		
		//OTHER STAFF
		
		$fields = new FieldList(array(
			$title,
			//OTHER STAFF
		));
		
		$validator = new RequiredFields(
            'Title'
			//OTHER STAFF
       	);
		
		$actions = new FieldList($search = new FormAction("AddObiekt", _t("AddObiektForm.ADD", "Dodaj obiekt i przejdź do dodawania zdjęć")));

		parent::__construct($controller, $name, $fields, $actions, $validator);
		
	}

	function forTemplate() {
		return $this -> renderWith(array(
			$this -> class,
			'Form'
		));
	}
	
	public function AddObiekt($data, $form) {
				
		if(Director::is_ajax()){
			return $this -> renderWith("Footer");
		} else { 
		
		//VALIDATE BY PHP
		
		if ($Obiekt) {
			//DO SOMETHING
		} else {
			//DO SOMETHING			
		}
		}
	}

}

JS

function tt() {
			$('#AddObiektForm_AddObiektForm_action_AddObiekt').click(function(e) {
			 	e.preventDefault();
			
			var $form = $("#AddObiektForm_AddObiektForm");
			
				var obiekt_id = $(this).data('obiektid');
				loading_show();
				$.ajax({
					type : "POST",
					url: "ajax/AddObiekt",
					data : $form.serialize()
				}).done(function(msg) {
					$('#sidebarMenuProfil').html(msg);
				}).error(function(msg) {
					alert('Błąd! Proszę spróbować ponownie lub skontaktować się z administratorem');
				});
			});
		}
		$(tt);