Get value of input field using php into variable

If you don't want to wait for the client to submit the form, you will need some javascript as PHP is a server-rendering language.

Basically you would need to set-up a listener on the input and after the client types the data in a format you want and you validate it, you can pass that to stripe script.


<th> 
    <form name="form" action='checkout.php' method='POST'>
         <input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">
         <input type="submit"> 
    </form>
</th> 

submit button in missing here, so add a submit button.

And update your PHP code

<?php $price = isset($_POST['price']) ? $_POST['price'] : 0; ?>

$_POST - is PHP Superglobals for getting form's post values. $POST (which you are using) is a normal PHP variable.

Please update your code like this.It will works.


You have used the wrong POST syntax, the correct is: $_POST, while you are trying get: $POST.

The docs: https://www.php.net/manual/en/reserved.variables.post.php

--In JavaScript part--

If you want to handle PHP the price before call Stripe, you should use other configuration, because this one will not work anyway.

You can:

  • call Stripe on like ajax or other request that in background post the forst
  • call Stripe on other page
  • don't do form, just plain text field (if you don't need PHP, handle price before Stripe request)

It depends what you want to do.