Как в форму передать сумму
Раньше я передавал сумму Python переменной. А сейчас я хочу чтоб пользователь сам вводил сумму. Как это сделать?
<script>
const buy_now_button = document.querySelector('#buy_now_btn')
buy_now_button.addEventListener('click', event => {
let formData = new FormData();
formData.append("price", {{ photography.price }}); // ВОТ СЮДА НАДО ПЕРЕДАТЬ СУММУ КОТОРУЮ ПОЛЬЗОВАТЕЛЬ САМ ВВЕДЕТ
formData.append("title", "{{ photography.title }}");
formData.append("photo", "{{ photography.license_file.url }}");
formData.append("photochanged", "{{ photography.photo_watermark.url }}");
formData.append("url", '{{ photography.get_absolute_url }}');
fetch('/checkout/', {
method: 'POST',
body: formData,
})
.then((result) => {
return result.json();
})
.then((data) => {
let stripe = Stripe(data.stripe_public_key);
stripe.redirectToCheckout({
{{CHECKOUT_SESSION_ID}}
sessionId: data.session_id
}).then(function (result) {
});
})
})
</script>
Ответы (1 шт):
Автор решения: nazarpunk
→ Ссылка
const $form = document.getElementById(`form`);
$form.addEventListener('submit', event => {
event.preventDefault();
fetch($form.action, {
method: $form.method,
body: new FormData($form),
})
.then(result => result.json())
.then(data => {
let stripe = Stripe(data.stripe_public_key);
return stripe.redirectToCheckout({
{
{
CHECKOUT_SESSION_ID
}
}
sessionId: data.session_id
})
})
.then(result => {})
})
<form action="/checkout/" method="POST">
<input type="number" name="price" value="0">
<input type="hidden" name="title" value="{{ photography.title }}">
<input type="hidden" name="photo" value="{{ photography.license_file.url }}">
<input type="hidden" name="photochanged" value="{{ photography.photo_watermark.url }}">
<input type="hidden" name="url" value="{{ photography.get_absolute_url }}">
<button type="submit">Отправить</button>
</form>