gpt4 book ai didi

javascript - 如何以类似于 onsubmit ="return javascript function"的形式在提交按钮上执行 php 函数?

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:56 24 4
gpt4 key购买 nike

我的目标:处理三个目标:(1) 在处理付款之前在登台表中插入。(2) 使用我的表单操作属性转到第三方支付处理站点,如 paypal。(3) 从支付登陆页面读取$_POST数据判断支付是否成功,然后从staging表中取出数据插入到real表中。

我想要的实现是这样的:

<form name="demo" method="post" onsubmit="<?php insertStagingData() ?>"       action="3rdpartyPaymentProcessingLink"> 
<input name ="Submit" type"submit" value"Submit">
</form>

我知道 Web 浏览器执行 onsubmit 处理程序,当处理程序完成时,浏览器继续提交表单。在将表单转发到 web 服务器进行 php 处理之前,我已经将这个概念用于 java 脚本客户端验证。

我不知道这是否可以在两个处理程序都需要服务器端处理时完成,一个在我的网络服务器上,第二个在第 3 方站点上。

在此先感谢您的建议和帮助!

最佳答案

无 js 解决方案的理论是这样的:

<form name="demo" method="post" action="/scriptOnYourServer.php"> 
<input name ="Submit" type"submit" value"Submit">
</form>

然后完全在 scriptOnYourServer.php 中你会先做任何你想要的验证,然后做登台表插入,然后使用第 3 方支付 PHP API(或 restful 服务或 soap 服务 - 任何可用的)来处理支付 -然后您应该从中获得成功/失败,并且您可以显示错误/提供重试或从暂存转移到真实表 - 所有这些都在对您的脚本的一个请求中。

如果您的目标是避免在 PHP 中编写支付 API 集成代码,那么您的基本大纲将仅适用于 javascript/AJAX,前提是您必须执行服务器端操作并且仍将表单操作转到支付处理器,并且通常是提供表单发布解决方案的支付 vendor 将允许“返回 URL”,他们可以将成功/失败发送到您进行进一步处理的地方,我认为您在第 3 步中描述了这一点。

所以这更像是:

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

参见 http://api.jquery.com/jQuery.ajax/

所以有了这几个事件部件 -您的脚本 - insertStagingData.php - 它的工作是读取发布数据,您应该进行某种验证,然后插入到您的登台数据库中(确保您正在使用参数化查询或清理数据,因为您正在将用户数据转储到数据库中),然后它会生成一个 json 响应,例如:

{"success":true}

{"success":false}

并且您需要让 PHP 使用 JSON header - 因此在此脚本的末尾您需要执行如下操作:

$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);

然后在上面的脚本 block 中的 jsFunctionName 中,您将从 json 响应中读取“成功”变量并显示错误并返回 false 或使用 preventDefault() 停止提交到第 3 方脚本,或者如果成功,您允许提交表单。

然后您可以为支付处理器设置一个单独的脚本,以回发到他们的文档将告诉您他们将向您发布哪些数据的位置,并且根据他们的成功/失败,您可以显示错误或成功,然后移动数据到真实表。

您可能还想考虑暂存/真实表应该是同一个表,其中有一列像 payment_success = 0 或 1,默认情况下它是 0,如果支付处理器回发成功,您只需将记录更新为payment_success=1。这应该是一个完全独立的表,几乎没有充分的理由。

这应该可以帮助您指明正确的方向。祝你好运

关于javascript - 如何以类似于 onsubmit ="return javascript function"的形式在提交按钮上执行 php 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com