19

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?

3 Answers 3

9

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));
Sign up to request clarification or add additional context in comments.

2 Comments

var data = {}; formData.forEach(function(v,k) { data[k]=v; });
Plus one for the last line to quickly debug values inside the formData object, very useful.
8

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

1 Comment

I think you might want to use @submit="search" or v-on:submit="search" on your form tag. At least for Vue 2.0 vuejs.org/v2/guide/events.html
7
  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.

1 Comment

with the new updated syntax you can use @submit="search"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.