Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » POST copy of Form to External

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

Page: 1
Go to End
Author Topic: 2053 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    POST copy of Form to External Link to this post

    I have a Custom Form that I need to write to the DB as well as POST to an external service (SalesForce). How do I map a copy of the Form contents to a Standard POST function? This is probably really simple and I'm just missing something basic.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: POST copy of Form to External Link to this post

    Two ways:

    1. Before the form is submitted, perform a javascript XHR to SalesForce. The downside is the XHR is submitted even if any server side validation fails.

    2. After the form is submitted, perform a cURL (or similar) server request to SalesForce submitting the POST data.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: POST copy of Form to External Link to this post

    You shouldn't use JS for this kind of thing as Hamish suggested - instead just create a HTTP request using cURL or similar.

    Edit: Sorry Hamish, didn't see you had already suggested that. So yeah, use the the second method Hamish suggested.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: POST copy of Form to External Link to this post

    Yeah, should add that another downside to 1 (using JS) is that browsers might think there is a security issue with the page, since it is trying to send data to another domain.

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: POST copy of Form to External Link to this post

    I'm also trying to post some form data to an external site. In this case, its SecurePay, to process membership payments. I'm just working locally at the moment, attempting to post to another page on the same site.

    Curl appears to work as claimed ie it sends off the request to the target page and returns the target page data to the originating page. However, this leaves the user on the original page. I want to send the user to the SecurePay site with all the necessary data in a POST request.

    In the past, I have used a work-around which is to show the user a second form, asking them to confirm their details, then using that to send to the payment site. This time around, I'd like to avoid the need for a second form, if possible.

    Is there something I've missed?

  • mobius
    Avatar
    Community Member
    54 Posts

    Re: POST copy of Form to External Link to this post

    There is an easier way:

    $myForm->setFormAction('http://blah');

    This causes the form to skip the normal SS routine and submit straight to a url

  • DsX
    Avatar
    Community Member
    178 Posts

    Re: POST copy of Form to External Link to this post

    anyone have an example of using curl for this??
    I have to integrate with saleforce as well...

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: POST copy of Form to External Link to this post

    This is what I'm using right now, Right now this form writes exclusively to sales force, though if you need to write to the DB as well you'd just need to create a new object and remap the fields

    function doWholesale($data, $form) {
          
          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, 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8');
          curl_setopt($ch, CURLOPT_POST, 1);
          
          curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
          
          curl_exec($ch);
          curl_close($ch);
          
          
          
       }

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