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.

Customising the CMS

comment creation in button function


Reply

6 Posts   1073 Views

Avatar
StarForce

31 October 2009 at 11:56am (Last edited: 1 November 2009 1:22am), Community Member, 11 Posts

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

31 October 2009 at 1:52pm Forum Moderator, 5511 Posts

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

31 October 2009 at 11:12pm Community Member, 11 Posts

ah ok, i didn´t know that

thanky you willr

it works now :)

Avatar
StarForce

1 November 2009 at 1:21am (Last edited: 1 November 2009 1:24am), Community Member, 11 Posts

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

1 November 2009 at 10:41am Forum Moderator, 5511 Posts

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

2 November 2009 at 3:30am Community Member, 11 Posts

thank you very much for your help :)

it works now