0

I have already refered to this question and the accepted answer did not work for me: How to get input field value using PHP

This is my code result.php file:

...
<th> 
    <form name="form" action='checkout.php' method='POST'>
         <input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">
    </form>
</th>  
<script
    src='https://checkout.stripe.com/checkout.js' class='stripe-button'
    data-key='key'
    data-amount=get value of input field with id price here
    data-name='Name'
    data-description='Description'
    data-currency='usd'
    data-locale='auto'>
</script>     
...

I have also tried fetching the value of the input like this and then using that variable:

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

Another method I tried:

<?php
$htmlEle = "<span id='SpanID'>Span Sports</span>";
$domdoc = new DOMDocument();
$domdoc->loadHTML($htmlEle);

$spanValue = $domdoc->getElementById('SpanID')->nodeValue;

I found the above snippet on https://phpcoder.tech/how-to-get-html-tag-value-in-php/ and modified it as per my need but did not work.

How can I do this? My app is pay what you want so I want the price to be filled in by the user on the client side.

I am open to different approaches and solutions to the one I asked for.

Odasaku
  • 177
  • 6
  • 17
  • It seems like you're trying to get the value before the user has entered it. You'll need to wait until they've submitted the form before rendering that stripe script, so that you can put a suitable value into the data-amount tag. – ADyson Jan 19 '22 at 07:19

5 Answers5

1

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.

1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Aurimas
  • 138
  • 7
1
<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.

Chetan Jha
  • 13
  • 4
0

I used a different method to solve the problem. I put the text box on a separate page and the pay button on the checkout page, so I am passing the price from the previous page to the checkout page now and accessing it using query parameters

Odasaku
  • 177
  • 6
  • 17
0
<form name="form" action='checkout.php' method='POST'>
         <input class='mx-2' type='number' id='price' name='price' placeholder='Donation Amount'">

<button name="submit" type="submit" >submit button</button>
   </form>

///in checkout.php code

if(isset($_POST['submit']){
    $price = $_POST['price'];
    echo $price;
}

///I hope this would work

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 31 '22 at 10:58