ajax laravel get values from form

Solutions on MaxInterview for ajax laravel get values from form by the best coders in the world

showing results for - "ajax laravel get values from form"
Silvia
08 Jan 2018
1<script>
2var form = $('#postAddProject');
3var button = $('#project-button');
4var name = $('#name');
5var link = $('#link');
6var image = $('#image');
7var token = $('input[name=_token]');
8var message = $('#message');
9
10var name_error = $('#name-error');
11var link_error = $('#link-error');
12var image_error = $('#image-error');
13
14button.click(function (event){
15
16event.preventDefault();
17
18var formData = new FormData();
19formData.append('name', name.val());
20formData.append('link', link.val());
21formData.append('image', image[0].files[0]); 
22
23$.ajax({
24url: form.attr('action'),
25method: 'post',
26dataType: 'json',
27contentType: false,
28processData: false,
29
30headers: {
31    'X-CSRF-TOKEN': token.val()
32},
33
34data: formData,
35
36error: function (data) {
37
38    if (data.status === 422) {
39
40         name_error.html(data.responseJSON.name);
41         link_error.html(data.responseJSON.link);
42         image_error.html(data.responseJSON.image);
43
44    } else {
45
46         alert('success');
47    }
48}
49</script>
50