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.
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.
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.
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.
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.
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?
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
anyone have an example of using curl for this??
I have to integrate with saleforce as well...
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);
}