2

I'm trying to make a tag-delete that works with checkboxes. Imagine the user have about 10 products and every product have 3 different tags.

Currently the user only can delete a single tag. After every tag-delete the page refreshes and he can delete another tag. this isn't really user-friendly. That's why I'm trying to do a multiple selection for tags and delete all the selected tags at one button-click.

That's how it looks right now:

{!! Form::open(['action' => 'ProductController@detach', 'method' => 'post']) !!}
<div class="container">
    <div class="table-responsive">
            <thead>
            <tr>
                <th>Product</th>
                <th>Tags</th>
            </tr>
            </thead>
            <tbody>
            @foreach($products as $product)
                <tr>
                    <td>{{ $product->name }}</td>
                    <td>
                        @foreach($product->tags as $tag)
                            {!! Form::hidden('tag_id', $tag->id) !!}
                            <input type="checkbox" name="xxxxx" value="{{ xxxx }}"><br>
                        @endforeach
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>

{!! Form::button('Delete') !!}
{!! Form::close() !!}

My problem is that I need the product_id + tag_id to detach them.

If I'm pressing the delete button, I'm getting this data passed to my controller:

{"tag_id":["285","284","285","286","279"],"product_id:"22"}

The main problem is, that my program just gives me the very last product_id. No matter if I haven't selected any tag from this product. It always gives me this product_id. I also need something like a multiple array passed to my controller to work with.

Something like this would be perfect:

[0] = product_id = 1 
      tag_id = 50,60,80
[1] = product_id = 2
      tag_id = 11,22,33 
....

My reply to your answer:

I'm sorry for my late answer..

I've tried it and it works nearly perfect. Only problem is that if two different products have the same tag, my programm will only return the last product with his tag. for example:

product_id 1 and product_id 2 have tag_id 3.

As an output I need: product_id1 : tag_id 3, product_id2 : tag_id 3

But I only get the last product_id with the tag passed, all other products with the same tag_id are ignored. So I'm just getting this as a result:

product_id2 : tag_id3.

I hope you understand what I'm trying to say.. my english isn't the best..

1
  • why don't you use a 'constructed' id - like the one you are already suggesting - for the value of the checkbox? Name the checkbox 'todelete' or something. The value is that product_id.'-'.tag_id. Then you should get an array into your controller like {'todelete':['22-285', '22-285', '23-284',...] which you can easily deconstruct again to delete the correct tags on the products. Commented Jul 25, 2016 at 13:59

2 Answers 2

1

You can pass the checkbox as an array field like this, with key as tag id and value is the product id,

@foreach($product->tags as $tag)
 <input type="checkbox" name="tagtodelete[{{ $tag->id.'-'.$product->id }}]" value="{{ $product->id }}"><br>
@endforeach

and in your controller you can get the value as an array ,

$tagsleftoffs = (array) $request->input('tagtodelete');
foreach($tagsleftoffs as $tagId => $productId){
//perform your action here
$productId = explode('-',$tagId);
echo $productId[0] ; //is tag Id
echo $productId[1] ; //is product Id
}
Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry for answering this late.. I had some problems with my connection last night - I will try it out !
note, on form submit, whatever checkboxes currently checked will be available in the request, so you can assume all other previously added tags which is not available in the post data, are deleted tags, so based on that you can update the database
please check my question. I've did an update to your answer. My code now is exactly like your answer
definitely you will receive only last, because you are using the same name attribute for the inputs in side the loop, you must have unique name for input or follow my suggestion and use name as array type, <input type="checkbox" name="xxxxx[]" value="{{ xxxx }}"><br>
tell me what i can change.. <input type="checkbox" name="tagtodelete[{{ $tag->id }}]" value="{{ $product->id }}"><br>
|
0

Form checkbox

You can pass tag id and product id through checkbox value.

@foreach($product->tags as $tag)
<input type="checkbox" name="tagProductInfos[]" value="{{ $tag->id.'-'.$product->id }}"><br>
@endforeach

Controller Receive fucntion:

And then, receive the value and separate the tag id, product id.

    foreach($request->get('tagProductInfos') as $tagInfo){
        $explodedData = explode('-',$tagInfo);
        $tagId = $explodedData[0] ; //is tag Id
        $productId = $explodedData[1] ; //is product Id
        echo 'tagId='.$tagId.'  productId='.$productId.'<br>';
    }

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.