【问题标题】:Use PHP function return value from onSubmit as a variable within in action for an HTML Form使用来自 onSubmit 的 PHP 函数返回值作为 HTML 表单中的变量
【发布时间】:2018-07-12 12:35:24
【问题描述】:

所以我有这样的东西(为了简单起见,是准系统):

<?php
    $paymentResult = 'Unknown';
    function processPayment(){
        // Process all payment values from form1 and retrieve the result of the transaction.
        $GLOBALS['paymentResult'] = 'Your payment was completed successfully!';
    }
?>                  

<form name="form1" method="POST" onsubmit="return processPayment();" action="<?php echo 'checkout_s.php?result=' . $paymentResult; ?>">
    <!--Additional Credit Card Payment Controls-->
    <input type="submit" value="Process Payment">
</form>

processPayment() 方法显然有额外的消息和逻辑,以防支付失败等。

我的问题是,无论我在 processPayment() 函数中将 $paymentResult 设置为什么,它总是将“未知”作为 当我在 checkout_s.php 上回显 $_REQUEST['result']; 时的 post 变量。

我非常有信心正确使用全局变量。有人可以就如何正确修改代码或提供其他方法提供建议吗?我不想将信用卡信息作为帖子变量传递,所以我在同一页面上处理它。我要做的就是将事务的结果传递到操作页面。

【问题讨论】:

标签: php html forms action onsubmit


【解决方案1】:

您的onsubmit 有问题。 onsubmit 上的值应该是javascript,但是你把processPayment() php 函数放在里面。

PHP 函数应该在 php 声明中。将onsubmit 值更改为此。

onsubmit="return <?php processPayment(); echo $paymentResult; ?>;"

【讨论】:

  • 啊,太愚蠢了。有时一个人只需要另一双眼睛。谢谢你,大卫。
  • 很高兴我能提供帮助。有时小错误会被忽视。
猜你喜欢
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
相关资源
最近更新 更多