7

I have a form that collects client's info and then it

  • saves them in the database
  • sends out few emails
  • sends the info to SalesForce (SF) department

Now, the first 2 two task was easy, but the third one is giving me trouble. For SF i don't have do anything special, just have to add their url and they will pull out all the necessary info they need from the form. To me it seems like in order to do these i need the form have 2 actions associated with one submit button (can't have 2 forms or 2 buttons).

So in my raf.php file I have this form, and after submission i have to redirect them to raf-submitted.php page that displays the success/error message and other necessary info.

The form (got the idea from here)

 <form id="referralForm" name="referralForm" autocomplete="off" enctype="multipart/form-data" method="POST">

 <input id="saveForm" name="saveForm" class="field-submit" type="button" value="Submit" style="padding-left:20px" width="100" tabindex="23" onclick="FormSubmission(); SalesForceSubmission();" />

 </form>

The JavaScript:

 $(document).ready(function()
 { 
    function SalesForceSubmission()
    {
       document.referralForm.action = "https://someAddress.salesforce.com/";
       document.referralForm.submit();          
       return true;
    }

    function FormSubmission()
    {
       document.referralForm.action = "raf-submitted.php";
       document.referralForm.submit();          
       return true;
    }
  });

The raf-submitted.php file take cares of the form validation, database insertion and email issues.

It's not working. I also tried these with no luck:

Can someone please help me? Thank you!

Community
  • 1
  • 1
AlwaysANovice
  • 983
  • 4
  • 15
  • 34

5 Answers5

2

Once a form is submitted, you are no longer on that page. You've navigated away.

The other way you can do this is submit the first action via AJAX, then submit the form naturally to the second destination. I would suggest using jQuery to make your AJAX calls since most of the AJAX code is already there for you to use.

Another option is to have raf-submitted.php perform a POST from your server to the salesforce server once it receives the form data. See: Post to another page within a PHP script

Community
  • 1
  • 1
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • unfortunately i am not that familiar with AJAX...let me take a look at what you've suggested – AlwaysANovice Jan 30 '13 at 22:08
  • Thank you. let me go through the tutorial :) – AlwaysANovice Jan 30 '13 at 22:25
  • when you said "_submit the first action via AJAX, then submit the form naturally to the second destination_" did you mean i do something like this for the SF: `$("#referralForm").ajaxSubmit({url: 'https://someAddress.salesforce.com/', type: 'post'})` and for raf-submitted page `
    `?
    – AlwaysANovice Jan 30 '13 at 23:21
  • Yes. Run the AJAX call first, then in the ONSUCCESS callback, call do $('#referralForm').submit(). If you don't do this via onsuccess you risk cancelling the first request before it completes. – Diodeus - James MacFarlane Jan 31 '13 at 15:18
  • +1 for the idea. Unfortunately I had to drop this issue and start working on something else. When I start working on it again, i'll give it a try again. Thank you for your time and help! – AlwaysANovice Feb 04 '13 at 22:18
2

The only way to do this in client side is using AJAX.
It is better to do SF stuff in server side (via DB if you have access or Web service if they have one or at least sending data via CURL at server side).
it is possible one of submissions fail in client side (and it seems to be bad if second one fail - first request has no idea that scond one is not done properly).


EDIT:
Sample code for posting with CURL:
assuming you have a form with a text and a hidden input:

<form method='post' action='http://your.sf.site/your_sf_submit_page.php'>
<input type='text' name='field1'/>
<input type='hidden' name='field2'/>
</form>

you can submit it using CURL like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://your.sf.site/your_sf_submit_page.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"field1=value1&field2=value2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

then you can check $server_output for being sure that form is submitted correctly.

Ariyan
  • 14,760
  • 31
  • 112
  • 175
  • no, i don't have access to that DB. and looks like there is no way around than using AJAX...i'm working on learning it now :) Thank you anyway. – AlwaysANovice Jan 30 '13 at 22:25
  • @Walahh: so, you can create your SF request in server side and submit it using CURL at server side – Ariyan Jan 30 '13 at 22:39
  • for SF all they gave me an url and a hidden input field to put in the form. can you plz show me some example/sample code for what you are exactly suggesting? TY! – AlwaysANovice Jan 30 '13 at 22:51
  • +1 for explaining it w/ example. Unfortunately I had to drop this issue and start working on something else. When I start working on it again, i'll give it a try. Thank you for your time and help – AlwaysANovice Feb 04 '13 at 22:16
0

If you have control over the raf-submitted.php page what I'd suggest is that you have an onclick action for the form which (after local validation) uses jQuery.ajax to call your page providing the field values as parameters in the query string (if using GET) then simply allow the submit process to continue passing the data to SalesForce.

If however you need to make the Salesforce activity invisible then you could call them the same way using type=POST and hand off to your form after that completes

Offbeatmammal
  • 7,970
  • 2
  • 33
  • 52
0
function fun_atualizar() {
    document.orcamento.action = "atualizar_orcamento.php";
    document.orcamento.submit();
    return true;
}

function fun_evento() {
    document.orcamento.action = "Evento_orc.php";
    document.orcamento.submit();
    return true;
}

<input  type="button" value="Atualizar" style="padding-left:20px" width="100" tabindex="23" onclick="javascript:fun_atualizar();" />

<input  type="button" value="Evento" style="padding-left:20px" width="100" tabindex="23" onclick="javascript:fun_evento();" />
Arturs
  • 1,258
  • 5
  • 21
  • 28
0

This looks a lot like something I was trying to do. You've already got an answer marked, but I don't think it's the simple answer you were looking for.

Assuming task 3 above can be done with a POST to some SalesForce .PHP location, and the first two tasks could be done in a JavaScript function, I think this will work:

<form method="post" action="SubmitToSalesForce.php">
  <button type="submit" name="doThreeThings" onclick="doTheFirstTwoTasks();" >
</form

<script type="text/javascript">
  function doTheFirstTwoTasks() {
    // Task 1
    // Task 2
  }
</script>

It first calls the doTheFirstTwoTasks() function via onclick= to do tasks 1 and 2, and then submits the form via a normal form submission to the action= location for the third task.

Nothing special, but I don't know how many people have learned that you can use onclick= and type=submit (with action=) for two different actions in a single form with a single submit button.

In the code above, it seems to call the onclick= before the action= (or perhaps it's just because the POST submission is slower than calling a JavaScript function...)

Appurist - Paul W
  • 1,291
  • 14
  • 22