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.

Customising the CMS /

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

comment creation in button function


Go to End


6 Posts   1726 Views

Avatar
StarForce

Community Member, 11 Posts

31 October 2009 at 11:56am

Edited: 01/11/2009 1:22am

Hello community,

i hope you can help me with the following problem:

i want to create a button and when you click on this button it should call a function.

So i tried this:
in page.php i added:

 
function showbutton() {
$button = new FormAction (
   $action = "doAction",
   $title = "Submit button"
);
 return $button;
}

   function doAction() {
	  my code.....
   }

in the template i add $showbutton.

when i open the page, i can see the button but nothing happens when i click on it.

Regards,
StarForce

Avatar
Willr

Forum Moderator, 5523 Posts

31 October 2009 at 1:52pm

For that to work the button has to be in a form object. You might have to do something like pass a blank field to to get the form to work

function showbutton() {
return new Form($this, 'showbutton', new FieldSet(new HiddenField('Hidden')), new FieldSet(new FormAction('doAction', 'Submit Button')));
}

Avatar
StarForce

Community Member, 11 Posts

31 October 2009 at 11:12pm

ah ok, i didn´t know that

thanky you willr

it works now :)

Avatar
StarForce

Community Member, 11 Posts

1 November 2009 at 1:21am

Edited: 01/11/2009 1:24am

hmm... now i have a new problem

i want to create a comment when i click on the button

when i add this to the do action function and click the button, no comment is beeing created...


	   $comment = new PageComment();
	   $comment->Name = "Name";
	   $comment->Comment = "DasIstDerComment";
	   $comment->ParentID = "1";
	   $comment->write();

but when i add it to showbutton it works (it creates a comment each time i visit the site) so the code itself should be right?

Thank you

StarForce

Avatar
Willr

Forum Moderator, 5523 Posts

1 November 2009 at 10:41am

Well does it call the doAction function at all? Put a die() in that function to see if it calls it at all.

Actually looking at your code I think it is because doAction() needs 2 parameters (one for the data, one for the form object) so try change the function line to..

function doAction($data, $form) {
...

Avatar
StarForce

Community Member, 11 Posts

2 November 2009 at 3:30am

thank you very much for your help :)

it works now