如何在类似于 onsubmit="return javascript function" 的表单中的提交按钮上执行 php 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20860049/
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
How do I execute php function on submit button in a FORM similar to onsubmit=" return javascript function"?
提问by user3122466
My goal: To process three objectives: (1) Make insertion in a staging table before processing payment. (2) Go to third party payment processing site like paypal using my form action attribute. (3) Read the $_POST data from the payment landing page to determine if payment was successful and then take the data from the staging table and insert into real table.
我的目标:处理三个目标:(1)在处理付款之前插入临时表。(2) 使用我的表单操作属性转到第三方支付处理站点,如贝宝。(3)从支付登陆页面读取$_POST数据,判断是否支付成功,然后从staging表中取出数据插入到实表中。
My desired implementation would sth like:
我想要的实现会像:
<form name="demo" method="post" onsubmit="<?php insertStagingData() ?>" action="3rdpartyPaymentProcessingLink"> <input name ="Submit" type"submit" value"Submit"> </form>
<form name="demo" method="post" onsubmit="<?php insertStagingData() ?>" action="3rdpartyPaymentProcessingLink"> <input name ="Submit" type"submit" value"Submit"> </form>
I know that the web browser executes the onsubmit handler and when the handler completes, the browser proceeds with the form submit. I have used this concept for java script client side validation before forwarding the form to web server for php processing.
我知道 Web 浏览器执行 onsubmit 处理程序,当处理程序完成时,浏览器继续提交表单。在将表单转发到 Web 服务器进行 php 处理之前,我已将此概念用于 java 脚本客户端验证。
I dont know if this can be done when both handlers require server side processing, one on my webserver and the second on a 3rd party site.
我不知道这是否可以在两个处理程序都需要服务器端处理时完成,一个在我的网络服务器上,第二个在第 3 方站点上。
Thanks in advance for suggestions and help!
在此先感谢您的建议和帮助!
回答by iamien
PHP is a server-side scripting language. which means it executes code when a page request is made. Thing like onclick browser events can not cause a webserver to run code.
PHP 是一种服务器端脚本语言。这意味着它在发出页面请求时执行代码。像 onclick 浏览器事件这样的事情不能导致网络服务器运行代码。
To facilitate this in PHP you will need to initiate the PHP function on the target form page.
为了在 PHP 中实现这一点,您需要在目标表单页面上启动 PHP 函数。
Look into Paypal APIs in order to have a more advanced implementation.
查看 Paypal API 以获得更高级的实现。
回答by codercake
The theory of a no-js solution would be something like this:
无 js 解决方案的理论是这样的:
<form name="demo" method="post" action="/scriptOnYourServer.php">
<input name ="Submit" type"submit" value"Submit">
</form>
And then completely within scriptOnYourServer.php you would do any validation you want first, then do the staging table insert, then use the 3rd party payment PHP API (or restful service or soap service - whatever is available) to process payment - which you should then get a success/fail from, and you either display errors/offer retry or move from staging to a real table - all within this one request to your script.
然后完全在 scriptOnYourServer.php 中,您将首先进行任何您想要的验证,然后进行临时表插入,然后使用第 3 方支付 PHP API(或restful 服务或soap 服务 - 任何可用的)来处理支付 - 您应该这样做然后从中获得成功/失败,您可以显示错误/提供重试或从暂存表移动到真实表 - 所有这些都在对您的脚本的这个请求中。
If your goal is to avoid writing payment API integration code in PHP, then your basic outline will only work with javascript/AJAX if you must perform server side actions and still have the form action go to the payment processor, and typically a payment vendor that offers a form post solution will allow a "return URL" they can send the success/fail to where you do further processing which I think you are describing in your step 3.
如果您的目标是避免在 PHP 中编写支付 API 集成代码,那么您的基本大纲将仅适用于 javascript/AJAX,如果您必须执行服务器端操作并且仍然让表单操作转到支付处理器,并且通常是支付供应商提供表单发布解决方案将允许“返回 URL”,他们可以将成功/失败发送到您进行进一步处理的位置,我认为您在步骤 3 中描述了这些。
So that is more like:
所以这更像是:
<form name="demo" method="post" onsubmit="jsFunctionName()" action="3rdpartyPaymentProcessingLink">
<input name ="Submit" type"submit" value"Submit">
</form>
<script type="text/javascript">
function jsFunctionName() {
// js ajax code, probably easiest to use a lib like jQuery for this
// You tell it the url on your server you will post vars to / generate a success/fail response, probably in json is best.
// based on your servers "insert into staging table" success/fail json response you decide whether to proceed or not (allow the submit to 3rd party), because you would not want to proceed if your staging table insert failed I presume
// so this implies you have a way to display errors in JS on this page, or you will be redirecting to a script of yours which would reload the page but display any errors - or you just completely ignore the possibility your staging data insert could fail and let the payment processor deal with it
}
</script>
See http://api.jquery.com/jQuery.ajax/
见http://api.jquery.com/jQuery.ajax/
So with this a few moving parts - your script - insertStagingData.php - it's job is to read the post data, you should do validation of some sort, then insert into your staging database (make sure you are using parametrized queries or sanitizing data since you are dumping user data into a database), then it generates a json response like:
因此,使用这几个移动部分 - 您的脚本 - insertStagingData.php - 它的工作是读取发布数据,您应该进行某种验证,然后插入到您的临时数据库中(确保您使用的是参数化查询或清理数据,因为您正在将用户数据转储到数据库中),然后它会生成一个 json 响应,例如:
{"success":true}
or
或者
{"success":false}
And you need to have PHP use a JSON header - so at the end of this script you need to do something like this:
并且您需要让 PHP 使用 JSON 标头 - 因此在此脚本的末尾您需要执行以下操作:
$response = array("success" => true); // or false depending on if your db insert was successful
header("HTTP/1.1 200 OK");
header('Content-type: application/json');
echo json_encode($response);
Then after that in the script block above in jsFunctionName, you will read the "success" variable from your json response and either display an error and return false or use preventDefault() to stop the submit to the 3rd party script, or if it is a success you allow the form submit to post.
然后在上面的 jsFunctionName 脚本块中,您将从 json 响应中读取“成功”变量,并显示错误并返回 false 或使用 preventDefault() 停止提交到第 3 方脚本,或者如果它是成功您允许表单提交发布。
Then you can setup a separate script for the payment processor to post back to where their docs will tell you what data they will post to you, and based on their success/fail you can display an error, or success and then move the data to the real table.
然后,您可以为支付处理器设置一个单独的脚本,将其发布回他们的文档将告诉您他们将发布给您的数据的位置,并且根据他们的成功/失败,您可以显示错误,或者成功,然后将数据移动到真正的桌子。
You may also want to consider the staging/real table should be the SAME table, with a column like payment_success = 0 or 1 where it is 0 by default and if the payment processor posts back success, you just update the record to payment_success=1. There are very few good reasons this should be a completely separate table.
您可能还想考虑暂存/真实表应该是 SAME 表,有一个类似 payment_success = 0 或 1 的列,默认情况下它是 0,如果支付处理器回发成功,您只需将记录更新为 payment_success=1 . 这应该是一个完全独立的表几乎没有充分的理由。
This should hopefully help point you in the right direction. Good luck
这应该有望帮助您指明正确的方向。祝你好运
回答by r3wt
what you can do is post the data inside of the same variable $_POST["TODO"]
to with javascript/ajax to a second page. on the second page, you parse the data, then submit it. it is possible to post an array with two get variables inside of it, each containing its own array of form info. hope this helps.
您可以做的是$_POST["TODO"]
使用 javascript/ajax将同一变量内的数据发布到第二页。在第二页,您解析数据,然后提交。可以发布一个包含两个 get 变量的数组,每个变量都包含自己的表单信息数组。希望这可以帮助。