【问题标题】:How do I intercept a form's values before sending it?如何在发送表单之前截取表单的值?
【发布时间】:2011-09-07 13:30:33
【问题描述】:

我目前有一个大表格发送给我们的付款授权人(通过 action="paymentautherizerURL" 完成),但是当我将交易存储在我的数据库。

我要么需要在提交之前截取表单数据,以便我可以将其存储在会话中(我们使用的是 PHP / jQuery),要么我也尝试将其发送到获取我需要的信息的中间小脚本,然后使用 jQuery 的 $.post() 重新构建并将数据发送给授权者。

但是,至少在我的最大努力下,第二种方法似乎不起作用。我不确定 $.post 是否正确模拟了表单的发送操作,或者至少我做得不对。

<?php
session_start();

$post = $_POST;

//gets all of the information that beanstream does not return to approved.php, but is still required to make
//a legitimate database entry. gets from the POST and stores in the session array for approved.PHP to access

$_SESSION['approvedArray']['billAddress'] = $_POST['ordAddress1'];
$_SESSION['approvedArray']['billProvince'] = $_POST['ordProvince'];
$_SESSION['approvedArray']['billCountry'] = $_POST['ordCountry'];
$_SESSION['approvedArray']['billPostalCode'] = $_POST['ordPostalCode'];
$_SESSION['approvedArray']['billCity'] = $_POST['ordCity'];

$_SESSION['approvedArray']['shipAddress'] = $_POST['shipAddress1'];
$_SESSION['approvedArray']['shipPostal'] = $_POST['shipPostalCode'];
$_SESSION['approvedArray']['shipCity'] = $_POST['shipCity'];
$_SESSION['approvedArray']['shipProvince'] = $_POST['shipProvince'];
$_SESSION['approvedArray']['shipCountry'] = $_POST['shipCountry'];

session_write_close();
//the javascript below will send what is required to beanstream as though it were sent from the form

<script type='text/javascript'>
$.post(, {
    <?php
    //rebuild the POST such that "name: value, " except the last name/value will not be followed by a comma
    $keys = array_keys($_POST);
    for($i = 0; $i < count($_POST); $i++) {
        $currentKey = $keys[$i];
        $currentPost = $_POST[i];
        echo $currentKey . ": " . $currentPost;
        if ($i < (count($_POST) - 1)) {
            echo ", ";
        }
    }
    ?>
});
</script> 

?>

通常,交易授权人会将用户重定向到 3 个页面之一(已批准、已拒绝、错误),我们的网站会从那里完成这项工作。但是,它目前卡在这个页面上,这让我觉得它没有正确发送。

我乐于接受各种形式的批评、方法和想法。提前非常感谢,如果需要任何其他信息,请告诉我!

【问题讨论】:

  • 您可以在发送之前处理数据
  • 是的,@lbu 说的。使用 onsubmit 回调的基本客户端输入验证。这是网络开发 101。
  • Ibu:这就是这个 scriptlet 的目的。它在 POST 中接收大量表单数据,处理它需要的内容,然后重新发送到它的实际目的地。
  • dossy:我忘了 onsubmit= 。这是否是运行此 scriptlet 但没有 jQuery 重定向的合适位置?谢谢。
  • 等等,你在关闭第一个标签之前启动了另一个 &lt;?php标签?

标签: php jquery forms post beanstream


【解决方案1】:

如何更改表单标签以包含 onSubmit 属性:

<form action="notmal_action.whatever" onSubmit="return save_data_function()">

save_data_function 从表单读取值并将其发送到服务器上的脚本以保存在数据库中(或任何地方)。我使用隐藏的 iframe 使这个请求对用户隐藏...

<script>
function save_data_function() {
$('#iframe_id').attr('src', 'data_saving_script.extension?data_1=' + $('form_data_1').val().serialize() + '&data_2=' + $('form_data_2').val().serialize());
}
</script>

如果数据没有足够快地传递到“data_saving_script.extension”文件,您可以设置超时。

【讨论】:

    【解决方案2】:

    由于您现有的示例已经依赖于 javascript,您可以转而使用 AJAX 保存数据,然后使用传统的提交向支付网关执行“真实”POST。

    假设你的表单有id="foo",你可以这样做:

    <script>
    $('form#foo').submit(function(event, doRealSubmit) {
        // this executes on the second pass
        if (doRealSubmit) {
            // returning true gets browser to do a real submit
            return true; 
        }
    
        // this executes on the first pass
        $.ajax({
            url: '/url/to/post/to/your/server',
            type: 'POST',
            // this serializes the form data in "application/x-www-form-urlencoded"
            data: $(this).serialize(),
            success: function(data) {
                // trigger 'submit' event again, but pass the doRealSubmit flag
                $('form#foo').trigger('submit', [true]);
            }
        });
    
        // returning false prevents browser from processing the real submit
        return false;
    });
    </script>
    

    【讨论】:

    • 另外,您可能希望手动构建data 属性,以便仅包含您实际要处理的字段。如果您避免将敏感值(如信用卡号)发送到您自己的服务器,则可以避免一些安全问题和责任。
    【解决方案3】:

    您应该将其发送到您自己的页面,而不是 action="paymentautherizerURL"

    <form action='process.php' method='post'>
    

    现在在您的 process.php 中,您可以处理数据(验证、过滤 ..)

    完成后,您可以使用cURL 将数据发送到正确的位置

    使用 curl 您可以发送帖子数据并等待响应来决定显示哪个页面。

    【讨论】:

    • 感谢伊布。目前,该操作实际上设置为这个脚本,而不是付款授权人,对不起,我想我的问题有点混淆了。
    • 您不需要使用 jquery 发送额外的表单,只需将其发送一次到您的脚本并使用数据即可。那么你的代码到底是什么不工作
    • 如果 POST 被正确发送给授权者,它将根据返回的数据重定向回适当的页面。目前,没有回应,所以它没有正确发送给授权人。我将如何使用 cURL 而不是 $.post 发送 POST?我认为使用 $.post 是问题所在,因为我认为它实际上并没有重定向页面
    • 不要使用 $.post() 只需验证您的表单,然后让提交按钮执行其默认操作。对于调试,在 process.php 上,执行var_dump($_POST) 以查看您是否正确发送数据
    • 对不起,您似乎误解了...验证发生在我在这里提到的任何事情之前。我只是想在发送给授权人之前获取一些表单输入,所以我正在使用这个中间脚本
    【解决方案4】:

    无需将数据放入会话中,提交表单时称为验证函数,在该函数中完成所有验证,然后使用 ajax 将数据发布到您的 process.php,然后它将保留在该页面上...

    【讨论】:

      【解决方案5】:

      不要将数据存储在会话中 - 这是保存事务数据的错误位置。

      将内容发布到您托管的脚本中,写入您需要保留到数据库中的内容,为其生成订单参考,

      那么……

      如果您使用支付处理器(例如 Paypal)

      重定向到在 URL 中传递订单引用的第二个脚本。在第二个脚本中,放入一个带有隐藏字段的表单,其中包含您的支付处理器所需的详细信息和一些自动提交表单的 javascript,并向用户发送一条消息,例如“正在连接到 Paypal...”

      如果您使用商家服务授权付款

      在您从登陆脚本生成输出之前,使用(例如)curl 将详细信息发送给您的授权人并解析响应,在数据库中记录针对订单的响应并通过网页向客户输出合适的消息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-29
        • 2015-04-07
        • 2014-07-25
        • 2019-11-18
        • 1970-01-01
        • 2019-08-31
        • 2015-02-20
        • 2012-03-31
        相关资源
        最近更新 更多