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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Form validation fails when posting form to external url


Go to End


4 Posts   1997 Views

Avatar
Optic Blaze

Community Member, 190 Posts

21 October 2014 at 1:03pm

Hi there,

I have a form that is supposed to post data to an external url and it does...but for some reason form validation does not work. If i add the domyform function in, the form does validate, but it does not post to the external url.

function myform() {
	$fields = new FieldSet(
	 new TextField('name_first','First Name'),
	 new TextField('name_last','Last Name'),
	 new EmailField('email_address','Email Address')
	 );
     // Create actions
	 $validator = new RequiredFields('name_first', 'name_last','email_address');
     	 $actions = new FieldSet(
         new FormAction('domyform', 'Pay Now')
        );
        $form = new Form($this, 'myform', $fields, $actions, $validator);
	 $form->setFormAction('http://www.blah.com');
	 return $form;
         /////this works, but validation fails
    }

the following validates...but does not post to external url

function myform() {
	$fields = new FieldSet(
	 new TextField('name_first','First Name'),
	 new TextField('name_last','Last Name'),
	 new EmailField('email_address','Email Address')
	 );
     // Create actions
	 $validator = new RequiredFields('name_first', 'name_last','email_address');
     	 $actions = new FieldSet(
         new FormAction('domyform', 'Pay Now')
        );
        $form = new Form($this, 'myform', $fields, $actions, $validator);
	 $form->setFormAction('http://www.blah.com');
	 return $form;
    }
  function domyform($data,$form) {
  return $form;
  }

Avatar
UncleCheese

Forum Moderator, 4102 Posts

22 October 2014 at 11:24am

Validation happens server side, not client side, so unless your external handler is validating the request, you'll have to do the validation with javascript. Alternatively, you can post to a local URL, e.g. your myform() handler, and forward the data to your external URL using cURL.

Avatar
Optic Blaze

Community Member, 190 Posts

22 October 2014 at 7:55pm

Edited: 22/10/2014 7:58pm

Thanks UncleCheese. I tried the following with the CURL option. It posts the data but the page that i am posting to is one of those payment gateways where the client completes the transaction on the gateway itself (client has to register etc). With the CURL option, i cant get the page to redirect to the new url, seems like CURL handles the entire POST transaction in my site. I did the following:

function domyform($form, $data) {
	$mydata = http_build_query($form); // Build POST string with form variables

	if (!function_exists('curl_init')) {
         echo "Error: $error\n";
         }
      // Create cURL
      $ch = curl_init();
      
      if (curl_error($ch) != "") {
          echo "Error: $error\n";
         }
		// Set URL
		curl_setopt($ch, CURLOPT_URL, 'http://www.blah.com');
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $mydata);
		curl_exec($ch);
		curl_close($ch); 		
		}

Avatar
Optic Blaze

Community Member, 190 Posts

22 October 2014 at 9:03pm

The following works...but i dont know if from a security point of view if it is a bad practice:

function domyform($form, $data) {
$mydata = http_build_query($form);
$url = 'http://www.blah.com';
header ('Location:'.$url.'?'.$mydata);
}