Laravel requires you to write your form like this.
<form method ="POST" action="whatever">
{{ csrf_field() }}
</form>
I've already created around 15 forms with jquery in my website with code like the following.
$("#formholder).append("<form id='myForm' method='post' action ='whatever'></form>");
My Question is:
How do I add the csrf field on these forms created with jquery?
Put this meta tag into the main Blade template with other meta tags:
Then you'll be able to access it with this from JS:
https://laravel.com/docs/5.5/csrf#csrf-x-csrf-token
Alternatively: Add following input field.
to the form
Updated: In your JQuery
Initial Global Var or create when new blade initialized using
<script>
tag. Add following code inside that script tag and use the variable in your JQuery.Step 1 : Add the following code to your main layout file.
Step 2 : Get the value of csrf token in your javascript.
Step 3 : In your form append a hidden input field with value set to your csrf variable
This has been worked for me.