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] Form submits to external url


Reply


3 Posts   3804 Views

Avatar
Optic Blaze

Community Member, 165 Posts

29 April 2011 at 12:39am

Hi there,

I need to create a form that posts data to an external url. When the user clicks submit, it must transmit the data and open the external url up in a seperate window.

This is what i have so far, but i just cant get the form to work properly:

class IBPayment extends Page {
   
   public static $db = array(
   );
}

class IBPayment_Controller extends Page_Controller {
   function IBPaymentForm() {
   //Create the fields that will be used
   $fields = new FieldSet (
      new OptionsetField ('liddesc', 'What are you paying for', array (
                     'Scuba dive course' => 'Scuba dive course',
                     'Casual dive' => 'Casual dive',
                     'Shark cage diving' => 'Shark cage diving',
                     'Equipment hire' => 'Equipment hire',
                     'Equipment purchase' => 'Equipment purchase'
                      )),
      new TextField('MerchCustom',"Unique Payment reference"),
      new TextField('lidprice',"Amount to pay")      
      );
   // Create actions
      $actions = new FieldSet(
         new FormAction('Submit','Pay Now'));
   // Create validator
   $validator = new RequiredFields('MerchCustom', 'lidprice', 'liddesc' );
         
   // Return form
   return new Form($this, 'IBPaymentForm', $fields, $actions, $validator);
      
   // Set the destination URL
      //$IBPaymentForm->setFormAction('http://blah/test.html');
      
      function Submit($data,$form) {
         $IBPaymentForm->setFormAction('http://blah/test.html');
         return Director::redirectBack();
         }
   }
}

Avatar
Optic Blaze

Community Member, 165 Posts

3 May 2011 at 11:21pm

Edited: 04/05/2011 1:27am

This one is written for the noobs....as they say it takes one to know one

It took me a hel-of-a long time to figure out that all i needed to do was to build the form into my template instead of doing fancy programming in the mysite->code->myform.php file. I saw a couple of people mention it in the forums, but saw no example of how it was done.

You see, all i wanted to do was build a page that could post some data to another website, but you dont need to go and build funny classes all you have to do is the following:

1. Create a different page type:

Go to mysite->code and create a new page type (lets call it -- myform.php.
Inside myform.php you extend the Page type like this:

<?php
class myform extends Page { }
class myform_controller extends Page_Controller {}
?>

What this does, is allows you to create a page in the CMS called myform which will store your form. (dont forget to dev/build)

2. Create the .ss template file that contains the form

now we create the myform1.ss template file in the directory theme->templates->includes

and build our html form in there:

<form action="http://www.myform.com" method="get" target="_blank" id="myform">
<input name="name" type="text">
<input type="submit" name="button" id="button" value="Submit">
</form>

3. Create the .ss template file that extends Page:

If we were to include the .ss file in step 2 into the Page.ss file it will render the form on every instance of 'Page" which is not what we want. So all we do is create a myform.ss file which is the class that we extended Page with. We do this by going into the layout folder, copying and pasting the page.ss file and renaming it to myform.ss. Then all you have to do is to include the the myform1.ss page in your myform.ss file like this <% include myform %>

AND THATS IT!

Avatar
Sphere

Community Member, 44 Posts

7 June 2011 at 9:24pm

This can be done easier:

In your form-function in the PHP, set $form->IncludeFormTag = false;

In your template:
<form action="the_url_you_want_to_post_to">
$YourFormName
</form>

And you've gotten yourself a form that's gonna send the data to an external url.