0

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

3
  • 1
    What about <input type="hidden" name="rooms[]" v-model="selected"> ? Commented Apr 9, 2020 at 16:46
  • Tried, Not working mate :) Commented Apr 9, 2020 at 16:50
  • When I select one room it's working, for multiple selections its showing null Commented Apr 9, 2020 at 16:52

1 Answer 1

2

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

Comments

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.