Javascript 一个表单,一个提交按钮,但有两个操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14614334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:23:56  来源:igfitidea点击:

One form, One submission button, but TWO actions

phpjavascripthtmlformsaction

提问by AlwaysANovice

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
  • 将它们保存在数据库中
  • 发送几封电子邮件
  • 将信息发送到 SalesForce (SF) 部门

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

现在,前两个两个任务很容易,但第三个任务给我带来了麻烦。对于 SF,我没有做任何特别的事情,只需要添加他们的 url,他们就会从表单中提取他们需要的所有必要信息。在我看来,为了执行这些操作,我需要表单有 2 个与一个提交按钮相关联的操作(不能有 2 个表单或 2 个按钮)。

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

所以在我的raf.php文件中我有这个表单,提交后我必须将它们重定向到raf-submitted.php显示成功/错误消息和其他必要信息的页面。

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:

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.phpfile take cares of the form validation, database insertion and email issues.

raf-submitted.php文件负责表单验证、数据库插入和电子邮件问题。

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

它不起作用。我也尝试过这些,但没有运气:

Can someone please help me? Thank you!

有人可以帮帮我吗?谢谢!

采纳答案by Diodeus - James MacFarlane

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.

另一种方法是通过 AJAX 提交第一个操作,然后自然地将表单提交到第二个目的地。我建议使用 jQuery 进行 AJAX 调用,因为大部分 AJAX 代码已经可供您使用。

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

另一种选择是在raf-submitted.php收到表单数据后从您的服务器向 Salesforce 服务器执行 POST。请参阅:在 PHP 脚本中发布到另一个页面

回答by RYN

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

在客户端执行此操作的唯一方法是使用 AJAX。
最好在服务器端做 SF 的东西(如果你有访问权限,通过 DB 或 Web 服务,如果他们有一个或至少通过服务器端的 CURL 发送数据)。
其中一个提交可能在客户端失败(如果第二个失败似乎很糟糕 - 第一个请求不知道第二个没有正确完成)。



EDIT:
Sample code for posting with CURL:
assuming you have a form with a textand a hiddeninput:

编辑:
使用 CURL 发布的示例代码:
假设您有一个带有 atext和一个hidden输入的表单:

<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:

您可以像这样使用 CURL 提交它:

$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_outputfor being sure that form is submitted correctly.

然后您可以检查$server_output以确保表单正确提交。

回答by Daniel LAcerda

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();" />

回答by Appurist - Paul W

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:

假设上面的任务 3 可以通过 POST 到一些 SalesForce .PHP 位置来完成,并且前两个任务可以在 JavaScript 函数中完成,我认为这会起作用:

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

它首先调用doTheFirstTwoTasks()via函数onclick=做任务1和任务2,然后通过普通的表单提交将表单提交到action=第三个任务的位置。

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

没什么特别的,但我不知道有多少人知道你可以在一个表单中使用onclick=andtype=submit(with action=) 执行两个不同的操作,并带有一个提交按钮。

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

在上面的代码中,它似乎调用了onclick=before action=(或者可能只是因为 POST 提交比调用 JavaScript 函数慢......)

回答by Offbeatmammal

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.

如果您可以控制 raf-submitted.php 页面,我建议您对表单进行 onclick 操作,该表单(在本地验证后)使用 jQuery.ajax 调用您的页面,提供字段值作为查询中的参数字符串(如果使用 GET)然后简单地允许提交过程继续将数据传递给 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

但是,如果您需要使 Salesforce 活动不可见,那么您可以使用 type=POST 以相同的方式调用它们,并在完成后移交给您的表单