Nick Davies
Nick Davies

Reputation: 653

Laravel DELETE request outside of form - Getting the data

I'm using this script to send a DELETE request using a button outside of a form. https://gist.github.com/nickdavies791/f547b64e2ad432c9d43ba93758ed168b

The form submits perfectly fine and adds the @csrf tag and the delete method to the form but no data is sent.

What I can't understand is how to append the data that I want to send to that form, because the form is appended to the <body> tag.

So basically what I have is this:

<!-- Submit button -->
<a href="{{ secure_url('assets/delete') }}" data-confirm="Are you sure?" data-method="DELETE">Dispose</a>

@foreach($assets as $asset)
    <table>
        <td><input type="checkbox" name="checkbox[]" value="{{ $asset->id }}"></td>
        <td>{{ $asset->name }}</td>
    </table>
@endforeach

<!-- Form that gets appended by script -->
<form method="POST" action="https://asset-manager.test/assets/delete">
  <input name="_token" value="HIV3JlPpUftvgrtcL9Irr3kIxMSFff6utpdeMSba" type="hidden">
  <input name="_method" value="DELETE" type="hidden">
</form>

Upvotes: 2

Views: 213

Answers (1)

Egretos
Egretos

Reputation: 1175

You can add an id to your form and then add <input form="form_id"> then use it anywhere you want. Even outside of form.

<form method="POST" id="my_app_form" action="https://asset-manager.test/assets/delete">
<!-- Hidden form fields -->
</form>

<!-- Any other markup here -->

<input form="my_app_form" type="text" name="my_data" value="{{ $your->data }}"/>

<!-- Any other markup here -->

Upvotes: 2

Related Questions