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

BikashKatwal's avatar

call controller method in javascript

method in controller

   public function update_invoice($id)
    {
        return 'success';
    }
    <div id="first-modal" class="reveal-modal" data-reveal>
                                        <h2>Confirmation</h2>
                                        <form>
                                            <button type="submit" class="next small-12 right">Confirm</button>
                                        </form>
                                        <a class="close-reveal-modal">&#215;</a>
                                    </div>
                                    <a class='open-first radius button>Paid</a>
    <script>
        $('a.open-first').on('click', function () {
            $('#first-modal').foundation('reveal', 'open');
        });
        $('a.close').on('click', function () {
            $('#first-modal').foundation('reveal', 'close', {

            });
        });
    </script>

I have to update it in the database, which I will write in the update_incoice function. but I want to call the controller's method in the javascript. How can I call the method?

or

Is there any way to route to the function when the button is clicked.

0 likes
4 replies
prasadchinwal5's avatar

@bikashkatwal All you have to do is define the method and action on the form tag.

Ex.

<form action="/route_to_your_controller_action" method="POST/GET">
<!-- Also do not forget to add your csrf_token -->
@csrf
<button type="submit" class="next small-12 right">Confirm</button>
</form>
BikashKatwal's avatar

@prasadchinwal5

 {{ Form::open(array('url' => 'admin/users/'.$data->id.'/update_invoice', 'method' => 'get')) }}
                                            <button type="submit" class="next small-12 right">Confirm</button>
                                        </form>

but it is redirecting to /update_route. instead I would just like to stay in the same url. This is a modal popup so I don;t want the page to redirect any where else.

prasadchinwal5's avatar

@bikashkatwal I see. In that case you might consider using an ajax request (axios library works best in my opinion). This would help you send request, perform actions(Store in db etc ) and retrieve response.

jlrdw's avatar

Just quick example:

<script>
    $(function () {
        $("#postjq").click(function (event)
        {
            event.preventDefault();
            var $post = {};
            $post.petid = $('#petid').val();
            $post.species = $('#species').val();
            $post.ocheck = ($("#ocheck").prop("checked") == true ? '1' : '0');
            $post._token = document.getElementsByName("_token")[0].value
            $post._method = document.getElementsByName("_method")[0].value
            alert($post.species);
            $.ajax({
                //url: 'http://localhost/laravel60/pet/petupdate',
                url: '<?= DIR . "pet/petupdate" ?>',
                type: 'PUT',
                data: $post,
                cache: false,
                success: function (data) {
                    alert('Your data updated');
                    return data;
                },
               error: function (data, ajaxOptions, thrownError) {
                    var status = data.status;
                    alert(status);
                    if (data.status === 422) {
                        $.each(data.responseJSON.errors, function (key, value) {
                            $('#msg').append('<div>' + value + '</div>');
                        });
                    }

                    if (status === 403 || status === 500) {
                        $('#msg').text("Not Auth");
                     }
                    //window.location.href = '<?//= DIR . "indexbl" ?>';
                }
            });
        });
    });
</script>

controller

    public function petUpdate(Request $request)
    {

        if (!ChkAuth::chkRole('user')) {   // ignore custom rbac
            abort(403);
        }
        $request->validate([
            'species' => 'required'
        ]);
        $petid = $request->input('petid');
        $species = $request->input('species');
        //$ocheck = $request->input('ocheck');
        $postdata = [
            'species' => $species
        ];
        DB::table('dc_pets')
                ->where('petid', $petid)
                ->update($postdata);
       
    }

form

<?php echo csrf_field(); ?>
<input type="hidden" name="_method" value="PUT">
.
.
.
<div id="msg">

</div>

To return to list

<a href="<?php echo $vurl; ?>">back</a>

$vurl is my variable to return, create your own.

Validate as needed.

Jeffrey has a free video on put request.

1 like

Please or to participate in this conversation.