2

in my view page i have a list of checkbox item in a table format. I want to send id of the selected checkbox(could be multiple id) when user click the add button to my controller. In the controller i will fetch some value and will pass it on to another view. below is my code

<div id="content">
    <div class="container">
        <div class="row">
            <div class="col-md-8">
                <div class="contact_form_holder">
                    {{ Form::open(array('url' => 'booking','METHOD' => 'POST')) }}
                    <div class="col-md-4">
                        {{form::text('checkin','',['class' => 'form-control','placeholder'=>'Check In Date','id'=>'checkin'])}}
                        <div id="error_datepicker" class="error">Please check again</div>
                    </div>
                    <div class="col-md-4">
                        {{form::text('checkout','',['class' => 'form-control','placeholder'=>'Check Out Date','id'=>'checkout'])}}
                        <div id="error_datepicker" class="error">Please check again</div>
                    </div>
                    <p id="btnsubmit">
                        <input type="submit" id="send" value="Search" class="btn btn-custom"/>
                    </p>
                    {{ Form::close() }}
                </div>
                <div class="col-md-12">
                    @if (isset($roomname))
                    <table class="table table-hover" data-toggle="table" id="table"
                           data-click-to-select="true">
                        <thead>
                        <tr>
                            <th data-field="state" data-checkbox="true"></th>

                            <th data-field="roomname">Room Name</th>
                            <th data-field="Desc">Description</th>
                            <th data-field="price">Price</th>
                            <th data-field="roomid" data-visible="false">Price</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($roomname as $value)
                        <tr>
                            <td>{!! $value->roomid !!}</td>
                            <td>{!! $value->roomname !!}</td>
                            <td>{!! $value->Desc !!}</td>
                            <td>{!! $value->price !!}</td>
                            <td>{!! $value->roomid !!}</td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                    @endif
                </div>
                <div class="col-md-12">
                    <br>

                    <button id="add_cart" class="btn btn-warning">Proceed to booking</a></button>
                    <!-- on click of this button i want the clicked item id from the table and send it to the controller and from the controller it shoud redirect the value to another page  -->
                </div>
            </div>

my javascript

$.ajax({
    url: '/roombooking',
    type: "get",
    data: {id: data},
    success: function (response) { // What to do if we succeed
        if (data == "success") {
        }
    },
    error: function (response) {
        alert('Error' + response);
    }
});
1

2 Answers 2

1

you can wrap your table inside form tag, and then use jquery serialize() method to get the all content for ex.

   @if (isset($roomname))
          <form id="testForm">
                <table class="table table-hover" data-toggle="table" id="table"
                       data-click-to-select="true">
                    <thead>
                    <tr>
                        <th data-field="state" data-checkbox="true"></th>

                        <th data-field="roomname">Room Name</th>
                        <th data-field="Desc">Description</th>
                        <th data-field="price">Price</th>
                        <th data-field="roomid" data-visible="false">Price</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($roomname as $value)
                    <tr>
                        <td>{!! $value->roomid !!}</td>
                        <td>{!! $value->roomname !!}</td>
                        <td>{!! $value->Desc !!}</td>
                        <td>{!! $value->price !!}</td>
                        <td>{!! $value->roomid !!}</td>
                    </tr>
                    @endforeach
                    </tbody>
                </table>
              </form>
                @endif

then in javascript use below

$.ajax({
    url: '/roombooking',
    type: "get",
    data: $('#testForm').serialize(),
    success: function (response) { // What to do if we succeed
        if (data == "success") {
        }
    },
    error: function (response) {
        alert('Error' + response);
    }
});

make sure you checkboxes are array in html so in all checkbox name attribute should be something like

<input type="checkbox" name="selected[]" />

then in your laravel controller you will get Request::all() will have 'selected' array

Sign up to request clarification or add additional context in comments.

2 Comments

how to access name selected[] in the controller
use Request::input('selected') will return the array of selected ids
1

For passing multiple id's from check boxes, you can do it by the help of arrays. Suppose (just for example) if there are 4 check-boxes on a page enclosed inside a form tag, you can implement the way - code below:

<form action="some_url" method="POST">
    <input type="checkbox" name="ids[]" value="1"> My Value 1
    <br />
    <input type="checkbox" name="ids[]" value="2"> My Value 2
    <br />
    <input type="checkbox" name="ids[]" value="3"> My Value 3
    <br />
    <input type="checkbox" name="ids[]" value="4"> My Value 4
    <br />
    <input type="submit" value="Submit">
</form>

So if you submit this form by checking all the check-boxes the output of posted data will be like:

<?php

    $inputs = request()->all();
    /*

     Inputs will have -
     $inputs = array(
        'ids' => array(1, 2, 3, 4);
     );

    */

?>

So you'll receive ids using request()->all() method of Laravel. Hope this will help you to solve your problem...!!

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.