Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » comment creation in button function

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

Page: 1
Go to End
Author Topic: 1048 Views
  • StarForce
    Avatar
    Community Member
    11 Posts

    comment creation in button function Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5483 Posts

    Re: comment creation in button function Link to this post

    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')));
    }

  • StarForce
    Avatar
    Community Member
    11 Posts

    Re: comment creation in button function Link to this post

    ah ok, i didn´t know that

    thanky you willr

    it works now

  • StarForce
    Avatar
    Community Member
    11 Posts

    Re: comment creation in button function Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5483 Posts

    Re: comment creation in button function Link to this post

    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) {
    ...

  • StarForce
    Avatar
    Community Member
    11 Posts

    Re: comment creation in button function Link to this post

    thank you very much for your help

    it works now

    1048 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.