Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » [SOLVED] How To make a form that posts to seperate url

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

Page: 1
Go to End
Author Topic: 633 Views
  • Bam
    Avatar
    Community Member
    16 Posts

    [SOLVED] How To make a form that posts to seperate url Link to this post

    Hi There

    I am rebuilding my company website site under SilverStripe and I have a real noob problem. I want to put a simple html form on a page, which logs into my cPanel and webmail servers, in a subdomain. In php it's extremely easy and well documented, as it's just a simple POST with the values 'user' and 'pass'.
    I have read the documentation on SilverStripe forms, but everything seems to relate to forms that store data in a db and then perform an action on that data. I don't want to store the data, simply to pass it onto my cpanel login page.
    Can anyone suggest how I would do this the SilverStripe way?

    Thanks
    Sam

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    Hi Bam,

    Sounds like you know how to build a form in silverstripe. All you should have to do is set the form action to your cpanel login address. You can do this using the setFormAction() function on your form object. See the API for more details: http://api.silverstripe.org/3.0/source-class-Form.html#888-897. Hope that helps.

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    Thanks IOTI, will give that a go!

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    So I've now got:

    <?php
    class CpanelLogin extends Page {
    }
    class CpanelLogin_Controller extends Page_Controller
    {
    static $allowed_actions = array(
    'CpanelLogin'
    );

    function CpanelLogin() {
            // Create fields         
             $fields = new FieldSet(
                 new TextField('user', 'Username'),
                 new TextField('pass', 'Password'),
             );
                 
             // Create Validators
             $validator = new RequiredFields('user', 'pass');

    $form->setFormAction('http://cpanel.myurl.com/login/');
    return $form
         }
    }

    But /dev/build stops silently on the Creating database tables at 'Page' step. Then no pages load.

    I think I may have this very wrong. Any tips as to where I've gone wrong?

    Thanks

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    Think I'm getting closer now.

    <?php
    class CpanelLogin_Controller extends Page_Controller {
    public function CpanelLogin() {
            $fields = new FieldList (
    TextField::create("user")
    TextField::create("pass")
    );
    $actions = new FieldList(FormAction::create("login")->setTitle("Log In"));
    $form->setFormAction('http://cpanel.myurl.com/login/');
         }

    }

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    Hey Bam,

    You're close! Missing a few key steps though. See my code below which should help.

    class CpanelLogin_Controller extends Page_Controller {
    public function CpanelLogin(){
          //Fields
          $fields = new FieldList(      
    new TextField("user"),
    new TextField("pass")             
          );
          
          //action field
          $actions = new FieldList(
             new FormAction('process', 'Log In')               
          );
          
          //required Fields
          $requiredFields = new RequiredFields(
        'user',
        'pass'                           
          );
          
          //creating form
          $form = new Form(
             $this,
             'CpanelLogin',
             $fields,
             $actions,
             $requiredFields            
          );
          
          $form->setFormAction('http://cpanel.myurl.com/login/');
          
          return $form;   
       }

    The key step you were missing was you were not creating a new form object, you were calling $form->setFormAction() on a non object which was probably causing your code to break.

    There's also a really good tutorial on making forms from the SS docs here: http://doc.silverstripe.org/framework/en/tutorials/3-forms (Maybe something you've already seen before).

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    Thanks IOTI!

    I will try and implement that now.

  • Bam
    Avatar
    Community Member
    16 Posts

    Re: [SOLVED] How To make a form that posts to seperate url Link to this post

    That worked!!!

    Thanks so much. I had read the doc on forms, but still didn't quite have my head around it. Understand it much better now thanks to you.

    633 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.