I am trying to set the amount in Stripe checkout (I am using Stripe SCK plugin for Kirby) dynamically, but I am struggling to pass the js variable to php (ajax). This is what I have so far:
1. Snippet âsck-qtyâ with integrated stripe script
The checkout has increment and subtract buttons to set the amount of products. I want to update the amount at every click via ajax in order to pass the amount to a php variable on the same site.
âŚSnippet âsck-qtyâ with integrated stripe script
1.) Get the article amount from user
2.) Send the amount via AJAX to a php variable on the same page
3.) Include the amount as a php variable into the generic Stripe script
The three steps happens in the same file (âsck-qty.phpâ) but I use âajax.phpâ for the ajax call and include the ajax snippet in the same file before the Stripe script.
Now with the current setting this doesnât seem to work. When loading the site, I get an undefined index error for: $currentamount = $_POST['current'];
in the ajax.php.
This is the first time I use ajax, so Iâm not sure, if I understand the concept completely. Does my setup makes sense at all?
Thanks @texnixe, this brought me a step further, the undefined index error is gone, but now I have an undefined variable ($currentamount) in the main snippet. It seems it canât get the variable from ajax.php?
ajax.php looks like that now:
if(r::is('post') and get('current')) {
$currentamount = $_POST['current'];
}
PHP variables seem to be the only one working in this generic Stripe script. I think I canât use the JS variables because the data-attribute âdata-amountâ (which I want to update) only takes strings. Or is there an other way to get the amount into it?
Page reload: I have an add and a subtract button, which each triggers the subt() and the incr() function with the ajax included. My idea was to call the ajax() function at every click and therefore to make an ajax call at every click in order to update the amount. Isnât the point of ajax, that I donât have to reload the page?
YEs, but that only makes sense if you get some data from the server to inject it into the page without reloading.
Of course, you can also send data to the server, but in this case, it doesnât seem to make sense because you are not doing anything with that data on the server, like storing it somewhere. So I donât see a point in doing that and would just inject the new value with JS
So I donât see a point in doing that and would just inject the new value with JS
Sure, I would like to keep it that simple but it doesnât work with JS variables. Is there maybe a way to set the data-amount value with JS from outside the Stripe script?
You could try Stripe Elements. This has more flexibility and youâll be able to set variables in JS. Youâll have to combine it with the Stripe-PHP wrapper. The SCK plugin could give some guidance.