-3

So I have something like this (barebone for simplicity of the question):

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

The processPayment() method obviously has additional messages and logic in case the payment fails and etc.

My question is, no matter what I set $paymentResult to within the processPayment() function, it always passes 'Unknown' as the value of the post variable when I echo $_REQUEST['result']; on checkout_s.php.

I'm pretty confident I'm using the global variable correctly. Can somebody please offer advice on how to correctly modify the code or offer an additional method? I don't want to pass credit card information as post variables, so I'm processing it on the same page. All I want to do is pass the result of the transaction to the action page.

david
  • 3,225
  • 9
  • 30
  • 43
SupraFast
  • 1
  • 3
  • Did you actually name your javascript and PHP function with the same name, or are you confusing client-side and server-side code and assume that `onsubmit="return processPayment() "` will somehow trigger a PHP function? – Mike Jul 12 '18 at 03:08
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – user3942918 Jul 12 '18 at 03:32
  • Yes, they are the same names. – SupraFast Aug 17 '18 at 18:50

1 Answers1

-1

There is something wrong in your onsubmit. The value on onsubmit should be about javascript, but you put processPayment() php function in it.

PHP function should be inside the php declaration. Change the onsubmit value into this.

onsubmit="return <?php processPayment(); echo $paymentResult; ?>;"
david
  • 3,225
  • 9
  • 30
  • 43