1

I'm a noob in Laravel, so please bear with me.

I'm continuing work on a friend's webpage that displays a list of items (with a checkbox beside each item). There are plenty of ways to interact with said items, such as "Delete", "Update", and the one I'm working on, "Download". There's a delete button per row/item/checkbox, so that a user can easily delete just one item. There's also a mass delete option, where the user can check multiple rows, check a "Delete" checkbox, and click on "Update". The webpage stores data on mongodb.

Supposedly, a user checks the box, clicks on Download, and a file is created and downloaded for the user.

But I'm not getting to that part yet. For now I'm having trouble even checking if the checkboxes are checked.

Here's my code:

Download Button:

<div class="col-md-1 col-md-offset-1">
    {{ Form::open( array(
            'url'    => 'contents/download',
            'role'   => 'form',
            'method' => 'POST',
            'class'  => 'form-inline'
        )
    ) }} 
    <div class="form-inline" role="form">
        <div class="form-group">
            <input type="submit" class="btn btn-success" name="download" id="download" value="Download Selected">
        </div>
    </div>
    {{ Form::close() }} 
</div>

Here's the rows of checkboxes/items. Notice that the value of the checkbox is a variable - it corresponds to the ID of the item in the database.

@foreach($children as $child) 
    @if($mother->id == $child->mother_id) 
        <tr>
            <td class="text-center">
                {{--*/ $child->id /*--}}
                <input type="checkbox" class="child" value="{{ $child->id }}" onclick=isSelected(this)>
           </td>

           <td>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-file"></span>
               <a href="{{ url($child_url) }}">{{ ucwords($child->child) }}</a>
           </td>
           <td class="text-center">
               Coder
           </td>
           <td class="text-center">
               {{ $child->updated_at }} 
           </td>
           <td class="text-center">
               {{ Form::open(array(
                       'url'    => 'contents/delete',
                       'role'   => 'form',
                       'method' => 'POST',
                       'class'  => 'form-inline'
                   )
               ) }}
               <input type="hidden" value="{{ $child->id }}" name="id">
               <input type="submit" class="btn btn-danger btn-xs" name="deleteChild" value="Delete" onclick="return confirmDelete();" />
               {{ Form::close() }} 
            </td>
        </tr>
    @endif
@endforeach

Controller Code:

public function postDownload()
{
    $input = Input::all();
    if(Input::has("child"))
    {
        echo $input["child"];
        // Begin download
    }
    else
    {
        echo "none";
        // Error - No Item selected
    }
}

My understanding of Laravel is basic at best. Where did I go wrong? Or perhaps there's another approach to this?

1
  • Always put questions in detail. You have not provided your routes, and the download button code on the very top does not make sense. Please elaborate your question. Commented Oct 13, 2014 at 9:30

2 Answers 2

1

First of all, in HTML you should give your checkboxes a name, perhaps something like items.

When you submit the form, Laravel get those data. And now if retrieve input by using Input::get('items'),

$itemIds = Input::get('items');

you will get an array whose elements are those checkboxes that were checked. This array is basically an indexed array, the structure could be like this

array(
    [0] => '1', // item 1 ID
    [1] => '5', // item 5 ID
    [2] => '33' // item 33 ID
)

Finally, you are ready to process $itemIds however you want.

By the way, if no boxes were checked in your UI, you cannot get an array by calling Input::get('items'). Therefore, it might be better to check whether this field exists first by calling Input::has('items').

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

Comments

0

In your HTML , name the checkboxes dynamically after your item IDs. Example , item1 , item2 etc.

Then check for selected items like this in your controller

$items = Item::all();

foreach($items as $item)
{
    if($request->has('item'.$item->id)){
        // item has been ticked
    }
    else{
       // item was not ticked
    }
}

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.