简体   繁体   中英

How Can I Post to 2 Domains on 1 Form Submit?

I have a form on my site that submits data to Salesforce, via php POST. At the same time I'm trying to POST the same data to my own site so I can store the info in my own database. How can I POST to both places with php?

Here's the form for reference:

<form class="gk-form" id="GK-Form" action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" novalidate="novalidate">
    <input type="hidden" name="oid" value="000000000000">
    <input id="ReturnUrl" type="hidden" name="retURL" value="https://exampleurl.com">
    <input id="company" type="hidden" name="company" value="examplecompany">
    <input id="lead_source" type="hidden" name="lead_source" value="LeadSource">
    <input class="sidebar-form valid" id="last_name" type="text" name="last_name" placeholder="First Name ..." required="">
    <input class="sidebar-form valid" id="email" type="email" name="email" placeholder="Email address ..." required="">
    <input class="sidebar-form" type="submit" name="lead_submit" value="Try It Free!">
</form>

you could turn it into an AJAX request, by overloading the onSubmit event, then you can run 2 ajax calls to each server capture the output from both, and go from there.

Or you can send the POST request to your server first, then your server can cURL the salesforce URL with POST type and the POST data as the fields in your form.

You can do this with jQuery, by making an ajax call.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#GK-Form").submit(function(e)
        {
            var postData = $("#GK-Form").serializeArray();
            var formURL0 = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8"
            $.ajax(
            {
                url : formURL0,
                type: "POST",
                data : postData,
                success:function(data, textStatus, jqXHR) 
                {
                    alert("post sent to first server");
                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                    //if fails      
                }
            });
            var formURL1 = "your second url"
            $.ajax(
            {
                url : formURL1,
                type: "POST",
                data : postData,
                success:function(data, textStatus, jqXHR) 
                {
                    alert("post sent to second server");
                },
                error: function(jqXHR, textStatus, errorThrown) 
                {
                    //if fails      
                }
            });


        });
    });

</script>

HTML form:

<form class="gk-form" id="GK-Form">
    <input type="hidden" name="oid" value="000000000000">
    <input id="ReturnUrl" type="hidden" name="retURL" value="https://exampleurl.com">
    <input id="company" type="hidden" name="company" value="examplecompany">
    <input id="lead_source" type="hidden" name="lead_source" value="LeadSource">
    <input class="sidebar-form valid" id="last_name" type="text" name="last_name" placeholder="First Name ..." required="">
    <input class="sidebar-form valid" id="email" type="email" name="email" placeholder="Email address ..." required="">
    <input class="sidebar-form" type="submit" id="submit_Button" name="lead_submit" value="Try It Free!">
</form>

Well, let me propose the following 3 simple steps:

  1. First, you need to make the form submit to your own script.
  2. Then do want ever you intented with the received data.
  3. Finally, when you're done, you use a HttpRequest object ( http://www.php.net/manual/de/class.httprequest.php ) to generate a request that forwards all of the data to the salesforce server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM