简体   繁体   中英

set PHP value before submit

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?

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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