0

I have one form but i want to submit to different route based on the link(button) the user clicks

`

{{ Form::open(['url'=> route('finance.invoices.store', ['pending', 'visit_id' => $visit->id]), 'id'=>'inv', 'method' => 'POST']) }}
        <script>
        $(document).ready(function() {
            $("#proforma").click(function() {
                // Code to execute when the "Send to Proforma" button is clicked
                console.log("Send to Proforma button clicked");
            });
            $("#bill-btn").click(function() {
                if ($("#inv").length) {
                $("#inv").attr("action", "{{ route('finance.invoices.store', ['pending', 'visit_id' => $visit->id]) }}");
                console.log("Bill Selected Items button clicked");
                // Submit the form with the specified action
                $("#inv")[0].submit();
                }else{
                    console.log('not found')
                }
            });`


`<v-btn type="submit" id="proforma" color="warning" class="px-2">
                            <i class="fa fa-money pr-2"></i>button 1
                        </v-btn>
                    
                <v-btn type="submit" id="bill-btn" color="success" class="px-2">
                    <i class="fa fa-money pr-2"></i> button 2
                </v-btn>
            </v-card-actions>

            <input type="hidden" name="removedItems" />

            {{ Form::close() }}


How can achieve such that when i click the `button 1 it submits to route 1 and same to button 2.

  • Does this answer your question? [Laravel form with two submit button](https://stackoverflow.com/questions/50997871/laravel-form-with-two-submit-button) – Skunka Mar 02 '23 at 10:25

1 Answers1

1

assign an id to your form, for example:

<form action="" method="" id="formsomething"></form>

assign listener to each button using jquery, for example:

<script>
    $('#btn1').on('click', function(){
        $('#formsomething').attr('action', '/urltargetA');
    });

    $('#btn2').on('click', function(){
        $('#formsomething').attr('action', '/urltargetB');
    });
<script>

you can change anything inside an html element using the "attr" function on jquery.

if you want to submit the form right after changing the target url, you can add this code tp the listener:

$('#formsomething').submit();