Reputation: 1243
I am using Formspree and Stripe Checkout. I'd like to have whatever's entered into the Formspree form's <input type="email" name="Customer Email" placeholder="your email here">
be sent to the data-email="[email protected]"
. Is this possible? My code looks like this:
<!-- Begin Stripe Form -->
<form method="POST">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_6pRNASCoBOKtIshFeQd4XMUh" data-image="" data-amount="8500" data-name="some co." data-description="Custom Blanket" data-locale="auto"
data-zip-code="true" data-billing-address="true" data-shipping-address="true" data-email="[email protected]">
</script>
<script>
document.getElementsByClassName("stripe-button-el")[0].style.display = 'none';
</script>
<button type="submit">Done</button>
</form>
<!-- End Stripe Form -->
<!-- Begin Formspree Form -->
<form action="https://formspree.io/xrqjnzam" method="POST">
<input type="text" name="_gotcha" style="display: none">
<input type="hidden" name="_next" value="https://example.com/blanket/thankyou.html" />
<input type="hidden" name="_subject" value="New Order!" />
<input type="hidden" name="_cc" value="[email protected]" />
<!-- <input type="hidden" name="_format" value="plain" /> -->
<input type="email" name="Customer Email" placeholder="your email here">
<button hidden type="submit">Send</button>
</form>
<!-- End Formspree Form -->
Upvotes: 2
Views: 274
Reputation: 17556
To send the typed email to the script
elements attribute, you have to add an event-listener.
$(function(){
var $email_field=$('[name="Customer Email"]');
$email_field.on('change', function(){
$('#script').attr('data-email', $email_field.val());
});
});
If it doesn't that is because the script is loaded already and initialized with the default email value [email protected]
. In that case you need to create the script with JavaScript after user types the email address and insert it into DOM.
$(function() {
var $stripe_form = $('#stripe_form');
var $email_field = $('[name="Customer Email"]');
$email_field.on('change', function() {
$('<script>').attr({
"src" : "https://checkout.stripe.com/checkout.js",
"class" : "stripe-button",
"data-key" : "pk_test_6pRNASCoBOKtIshFeQd4XMUh",
"data-image" : "",
"data-amount" : "8500",
"data-name" : "Company",
"data-description" : "Product",
"data-locale" : "auto",
"data-zip-code" : "true",
"data-billing-address" : "true",
"data-shipping-address" : "true",
"data-email" : $email_field.val(),
}).prependTo($stripe_form);
});
});
But obviously this will add a new <script>
every-time user changes the email. So it will be better if you hide the email section of your form when user enters the email and confirms it with a button click.
$(function() {
var $stripe_form = $('#stripe_form');
var $email_section = $('#email_section');
var $email_field = $('[name="Customer Email"]');
var $email_confirm_button = $('#email_confirm_button');
$email_confirm_button.on('click', function() {
$email_section.slideUp();
$stripe_form.show();
$('<script>').attr({
"src": "https://checkout.stripe.com/checkout.js",
"class": "stripe-button",
"data-key": "pk_test_6pRNASCoBOKtIshFeQd4XMUh",
"data-image": "",
"data-amount": "8500",
"data-name": "Company",
"data-description": "Product",
"data-locale": "auto",
"data-zip-code": "true",
"data-billing-address": "true",
"data-shipping-address": "true",
"data-email": $email_field.val(),
})
.prependTo($stripe_form);
});
});
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<form action="https://formspree.io/xrqjnzbm" method="POST">
<input type="text" name="_gotcha" style="display: none">
<input type="hidden" name="_next" value="https://example.com/thankyou.html" />
<input type="hidden" name="_subject" value="New Order" />
<input type="hidden" name="_cc" value="[email protected]" />
<div id="email_section">
<input type="email" name="Customer Email" placeholder="your email here">
<button id="email_confirm_button" type="button">Continue to Payment</button>
</div>
<button type="submit" hidden>Send</button>
</form>
<form id="stripe_form" method="POST" style="display:none">
<button type="submit">Continue to Next Step</button>
</form>
Upvotes: 1
Reputation: 10148
This is how I would have done it. Assign any id to script
tag for example
<script id="script" .. ></script>
Now what you need is to get the email
value from form field and set to script tag as custom or whatever attribute. Something like
var email = document.querySelector('input[type="email"]').value;
document.getElementById("script").setAttribute('data-email', email);
Upvotes: 1