繁体   English   中英

在提交之前设置PHP值

[英]set PHP value before submit

我正在尝试集成使用设定金额的交易表单。 对于这个项目,我真的需要使交易金额灵活 - 可由用户编辑。 这是我需要调整的代码(我将它修剪了一下以显示关键部分)

<?php
require_once 'anet_php_sdk/AuthorizeNet.php';
$amount = "5.99";
?>

<form method='post' action="https://secure.authorize.net/gateway/transact.dll">
<input type='hidden' name="x_amount" value="<?php echo $amount?>" />
<input type='submit' value="Click here for the secure payment form">
</form>

我基本上想让“x_amount”变量成为文本输入而不是隐藏; 我需要编码PHP中的$ amount以匹配用户键入表单输入字段的代码,然后按正常方式提交表单...

我认为这可能是一个ajax / JS onbeforesubmit钩子,但不清楚如何编码? 或者有更优雅的方式?

您需要一个脚本发布到另一个设置$ amount值的脚本。

setamount.php

<form method='post' action="confirm.php">
<input type='text' name="amount" value="" />
<input type='submit' value="Click here for the secure payment form">
</form>

confirm.php

<?php
require_once 'anet_php_sdk/AuthorizeNet.php';
$amount = $_POST['amount'];
?>


<form method='post' action="https://secure.authorize.net/gateway/transact.dll">
<input type='hidden' name="x_amount" value="<?php echo $_POST['amount']; ?>" />
<input type='submit' value="Click here for the secure payment form">
</form>

这似乎可以解决您已经确定的问题,但我不确定您使用的API是否打算以这种方式使用,并且可能有更好的解决方案。

你至少需要一个文本文件。 可以使用JS文件

// js file
var amount = "5.99";
window.onload=function() {
  document.forms[0].x_amount.value=amount; // assuming first form on page
}


<script src='amount.js"></script>
<form method='post' action="https://secure.authorize.net/gateway/transact.dll">
<input type='hidden' name="x_amount" value="" />
<input type='submit' value="Click here for the secure payment form">
</form>

我已经完成了这个并且在声明了一个变量并在页面加载后给它一个值然后我可以在我需要的任何其他地方使用该值。

<?php
   //use some sort of escape for security reasons but thats another issue aside..
   $amount = mysql_escape_string($_POST['amount']);
?>
<html>
<head>
</head>
<body>
 <form action="" method="post">
  <input type="text" value="<?php $amount ?>"/>
 </form>
</body>
</html>

这应该工作..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM