haakym
haakym

Reputation: 12358

Submitting form in Vue.js with ajax

I'm really stuck on how I would work with submitting a form that makes an ajax request using Vue.js and vue-resource then using the response to fill a div.

I do this from project to project with js/jQuery like this:

view in blade

{!! Form::open(['route' => 'formRoute', 'id' => 'searchForm', 'class' => 'form-inline']) !!}
    <div class="form-group">
        <input type="text" name="id" class="form-control" placeholder="id" required="required">
    </div>
    <button type="submit" class="btn btn-default">Search</button>
{!! Form::close() !!}

js/jquery

var $searchForm = $('#searchForm');
var $searchResult = $('#searchResult');

$searchForm.submit(function(e) {
    e.preventDefault() ;

    $.get(
        $searchForm.attr('action'),
        $searchForm.serialize(),
        function(data) {
            $searchResult.html(data['status']);
        }
    );
});

What I've done/tried so far in Vue.js:

view in blade

{!! Form::open(['route' => 'formRoute', 'id' => 'searchForm', 'class' => 'form-inline']) !!}
    <div class="form-group">
        <input type="text" name="id" class="form-control" placeholder="id" required="required">
    </div>
    <button type="submit" class="btn btn-default" v-on="click: search">Search</button>
{!! Form::close() !!}

vue/js

    Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

    new Vue({
        el: '#someId',

        data: {

        },

        methods: {
            search: function(e) {
                e.preventDefault();

                var req = this.$http.get(
                    // ???, // url
                    // ???, // data
                    function (data, status, request) {
                        console.log(data);
                    }
                );
            }
        }
    });

I'm wondering if it's possible to use components when dealing with the response to output the response data to a div?

Just to summarise everything:

  1. How do I submit a form using vue js and vue-resource instead of my usual jQuery way?
  2. Using a response from ajax, how can I output data into a div preferably using components?

Upvotes: 19

Views: 49185

Answers (3)

Anish Dcruz
Anish Dcruz

Reputation: 535

In order to get the value from input you have to use v-model Directive

1. Blade View

<div id="app">
<form v-on="submit: search">
    <div class="form-group">
        <input type="text" v-model="id" class="form-control" placeholder="id" required="required">
    </div>
    
    <input type="submit" class="btn btn-default" value="Search">
</form>
</div>

<script type="text/javascript">

// get route url with blade 
var url = "{{route('formRoute')}}";

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');


var app = new Vue({
    el: '#app',
    data: {
        id: '',
        response: null
    },
    methods: {
        search: function(event) {
            event.preventDefault();
            
            var payload = {id: this.id};

            // send get request
            this.$http.get(url, payload, function (data, status, request) {

            // set data on vm
            this.response = data;

            }).error(function (data, status, request) {
                // handle error
            });
        }
    }
});
</script>

If you want to pass data to component the use 'props' see docs for more info

http://vuejs.org/guide/components.html#Passing_Data_with_Props

If you want use laravel and vuejs together, then checkout

https://laracasts.com/series/learning-vuejs

Upvotes: 8

Samuel De Backer
Samuel De Backer

Reputation: 3461

  1. Add v-model="id" on your text input

  2. then add it to your data object

    new Vue({
        el: '#someId',
    
        data: {
            id: ''
        },
    
        methods: {
            search: function(e) {
                e.preventDefault();
    
                var req = this.$http.get(
                    '/api/search?id=' + this.id,
                    function (data, status, request) {
                        console.log(data);
                    }
                );
            }
        }
    });
    
  3. It’s better to remove v-on="click: search" and add v-on="submit: search" on the form tag.

  4. You should add method="GET" on your form.
  5. Make sure you have #someId in your html markup.

Upvotes: 7

scrubmx
scrubmx

Reputation: 2556

I used this approach and worked like a charm:

event.preventDefault();

let formData = new FormData(event.target);

formData.forEach((key, value) => console.log(value, key));

Upvotes: 9

Related Questions