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.

Form Questions /

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

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


Go to End


8 Posts   1638 Views

Avatar
Bam

Community Member, 16 Posts

25 July 2013 at 11:58pm

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
copernican

Community Member, 189 Posts

26 July 2013 at 12:17am

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.

Avatar
Bam

Community Member, 16 Posts

26 July 2013 at 11:21am

Thanks IOTI, will give that a go!

Avatar
Bam

Community Member, 16 Posts

26 July 2013 at 1:31pm

Edited: 26/07/2013 2:57pm

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

Community Member, 16 Posts

26 July 2013 at 3:45pm

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
copernican

Community Member, 189 Posts

27 July 2013 at 12:11am

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

Avatar
Bam

Community Member, 16 Posts

29 July 2013 at 3:33pm

Thanks IOTI!

I will try and implement that now.

Avatar
Bam

Community Member, 16 Posts

29 July 2013 at 3:41pm

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.