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.

Form Questions

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


Reply

8 Posts   646 Views

Avatar
Bam

25 July 2013 at 11:58pm Community Member, 16 Posts

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

Avatar
IOTI

26 July 2013 at 12:17am Community Member, 189 Posts

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: [url]http://api.silverstripe.org/3.0/source-class-Form.html#888-897[/url]. Hope that helps.

Avatar
Bam

26 July 2013 at 11:21am Community Member, 16 Posts

Thanks IOTI, will give that a go!

Avatar
Bam

26 July 2013 at 1:31pm (Last edited: 26 July 2013 2:57pm), Community Member, 16 Posts

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

Avatar
Bam

26 July 2013 at 3:45pm Community Member, 16 Posts

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

}

Avatar
IOTI

27 July 2013 at 12:11am Community Member, 189 Posts

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: [url]http://doc.silverstripe.org/framework/en/tutorials/3-forms[/url] (Maybe something you've already seen before).

Avatar
Bam

29 July 2013 at 3:33pm Community Member, 16 Posts

Thanks IOTI!

I will try and implement that now.

Avatar
Bam

29 July 2013 at 3:41pm Community Member, 16 Posts

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.