Reputation: 337
i am trying to integrate a transaction form that uses a set amount. for this project, i really need to make the transaction amount flexible - editable by the user. here's the code i need to tweak (i trimmed it down a bit to show the key parts)
<?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>
i basically would like to make that "x_amount" variable a text input instead of hidden; i need code which would edit the $amount in PHP to match the one the user types into the form input field, then submits the form as normal...
i think this might be possible with an ajax / JS onbeforesubmit hook, but not clear how to code that? or maybe there's a more elegant way?
Upvotes: 3
Views: 3084
Reputation: 2023
I've done this and after declaring a variable and giving it a value as soon as the page is loaded then I can use that value anywhere else I've needed to.
<?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>
and that should work..
Upvotes: 0
Reputation: 5885
You need a script that posts to another script that sets the $amount value.
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>
This appears to solve the issue you have identified, but I am not sure that the API you are using was intended to be used this way and there is probably a better solution.
Upvotes: 2
Reputation: 178061
you need at least a text file. JS file could be used
// 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>
Upvotes: 0