Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 947
dubesor Avatar asked Aug 05 '26 07:08

dubesor


1 Answers

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.

like image 144
Kieran Andrews Avatar answered Aug 06 '26 22:08

Kieran Andrews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!