Home » vue » How to add multiple form values using vuejs?

How to add multiple form values using vuejs?

Posted by: admin November 26, 2021 Leave a comment

Questions:

select_room.blade.php

<input type="hidden" name="rooms[]" :value="selected">

app.js

const app = new Vue({
    el: '#panel',
    data: {
        selected: []
    },
    methods: {
        selectRoom(room) {
            if (!this.selected.find(room => room)) {
                this.selected.push(room);
            } else {
                this.selected.pop(room);
            }

            console.log(this.selected);
        }
    }
});

rooms

When I click on room buttons (eg: 105,106) room numbers will be added to selected array. But when
I check my $request->rooms its showing null. I just wanted to how to add multiple values to name=rooms[] using vue.js
Thanks in advance!

error

Answers:

I didn’t setup a PHP/Blade/Vue.js environment but the following seems to be achieving what you’re looking for. I got a rooms GET variable with coma separated selected values.

<div id="app">
    <form method="GET">
        <button @click.prevent="selectRoom(1)">1</button>
        <button @click.prevent="selectRoom(2)">2</button>
        <button @click.prevent="selectRoom(3)">3</button>
        <input type="hidden" name="rooms" :value="selected" />
        <input type="submit" text="submit">
    </form>
</div>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            selected: [],
        },
        methods: {
            selectRoom(room) {
                if (!this.selected.includes(room)) {
                    this.selected.push(room);
                } else {
                   this.selected.pop(room);
                }
                console.log(Object.values(this.selected));
            }
        },
    }
)
</script>