Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Mounting Stripe Elements, in foreach modal(s)

Hi all,

I have a foreach loop showing a list of "bills" and each one can have a Pay by Card link, which opens the Bootstrap modal window.

I need to show the Stripe Elements card div but it needs to relate to the current opened modal. I guess I can hook into the modal event, but I'm not sure how to, or what should I wrote to make this work. Can any of you wonderful people help me out?

This is the modal inside the foreach loop:

<!-- Modal -->
                            <div class="modal fade" id="item_{{$item->id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">Pay Bill</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="bg-light p-3">
                                            
                                            <h4>Total (inc fees) {{ currency(card_fee($item->amount_outstanding)) }}</h4>
                                        </div>
                                        <div class="form-group">
                                            <label>Cardholder Name</label>
                                            <input id="card-holder-name" class="form-control" type="text">

                                            <!-- Stripe Elements Placeholder -->
                                            <div id="card-element"></div>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                        <button id="card-button" data-secret="{{ $intent->client_secret }}" class="btn btn-success">Pay Now</button>
                                    </div>
                                </div>
                            </div>
                            </div>

Then I have this JS, which does render in the first modal, but doesn't show in the other modals if there's more than one in the foreach loop.

<script>
    const stripe = Stripe('pk_test_xxxxxxxxxxxxxxx');
    const elements = stripe.elements();
    const cardElement = elements.create('card');

    cardElement.mount("#card-element");
</script>
0 likes
7 replies
theUnforgiven's avatar

So, I guess my question is how to I mount the stripe element to each modal should there be more than one within the loop.

theUnforgiven's avatar

Hey @martinbean The modal is just in blade in a foreach loop and I've added the id of each loop item to the modal so that works fine, it was just adding the Stripe elements part that is my problem.

theUnforgiven's avatar

Ah, good point I guess, must have missed that. Anyhow I've gone a different route for now which will work just fine for what I need.

1 like
jamesautodude's avatar

I'm still having this issue even now. I have a stripe element within a foreach loop, and I need it that way so I can submit the payment for the specific invoice ID and update the database after successful payment, but nothing I try is working :/

I guess I'll have to just make a button to pass the invoice ID to 1 stripe element somehow, but then I can't have the form with every invoice shown. Or I have to just make a separate invoice page I guess, but was trying to just have it all on one page (small amount of info shown)

Please or to participate in this conversation.