1779 Posts in 582 Topics by 556 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 879 Views |
-
Display form via ajax response

7 August 2011 at 11:43pm
Hello,
I request some help with the following. I was to display a form using ajax when a user clicks a certain link. I managed to get it working when I just let ajax respond with some plain text, however not with a form. I tried the following:
ajaxSnippet.ss (template file)
$Form
Page.php (code file)
$form = new Form($this, 'Form', ....);
return $form->renderwith('ajaxSnippet');Where the Form(....) is defined as normal, with just a textfield and a submit button. I think my error is in the template file, that I might need to reference not to $Form but something else, only I can't think of it.
Any help appreceated,
Maurice -
Re: Display form via ajax response

8 August 2011 at 12:02am
Have one function for the form (which you already have) and then have a separate method for sending the form back.
public function FileUploadForm() {
$fileUploadField = new FileUploadField('ProjectFile', "Upload a file");
$fileUploadField->setUploadFolder($this->projectFolder);
$fields = new FieldSet(
new HiddenField("ID"),
new HiddenField("ParentID"),
$fileUploadField
);
$actions = new FieldSet(
new FormAction('doUploadFile', 'Upload')
);
return new Form($this, "FileUploadForm", $fields, $actions);
}function ajax_file_form() {
$form = $this->FileUploadForm();
return $form->forTemplate();
}In this case I have a function called FileUploadForm which constructs a form and returns it in a normal manner. The function ajax_file_form() then calls that function and returns the form as a string with the $form->forTemplate() method.
If you want a different template for the actual form then use $form->setTemplate() to set a new template. You have to have a function (as far as I know) that returns a new Form for the form handling to work properly. If I'd wanted to just print that form in a template I'd write $FileUploadForm to show it. -
Re: Display form via ajax response

8 August 2011 at 12:10am
Thank you, that worked for me (the forTemplate() method). Have a nice day.
-
Re: Display form via ajax response

11 August 2011 at 1:23am
Hi,
it looks like the normal way to handle a form does not work when it is called using ajax? I have in my pageController:
function showProductDetails($action) {
$productNumber = Director::urlParam("OtherID");
$referer = Director::urlParams();
$Link = $referer["URLSegment"]."/".$referer["Action"];
if($this->isAjax) {
// Get product details
$details = DataObject::get_one("Article","ID = $productNumber");
// Get product contents
$contents = DataObject::get("ArticleChilds","ArticleID = $productNumber AND stock > 0");
if(!empty($contents)) {
$map = $contents->map('ID','Name');
$fields = new FieldSet(
new DropdownField('ID', 'Maak een keuze:', $map)
);
} else {
$fields = new FieldSet(
);
}$actions = new FieldSet(
new FormAction('doSubmitNewItem','Submit')
);$form = new Form(
$this,
'showProductDetails',
$fields,
$actions
);
return $form->forTemplate();
} else {
return array();
}
}function doSubmitNewItem($data,$form) {
// Save the form datareturn;
}However this does not in any way I try below the // Save the form data, actually save the form data. Do I make use of incorrect references to the save-function?
Thank in advance,
Maurice -
Re: Display form via ajax response

11 August 2011 at 1:48am
That's why I talked about having a separate function that grabs the form and returns the output for AJAX requests. Your form function has to always return a form object otherwise the form submissions won't work. So the AJAX function calls the form function and returns forTemplate() and the form function returns the form. If you do it like that everything should work.
| 879 Views | ||
|
Page:
1
|
Go to Top |

