mxbn's avatar
Level 1

Submit & get multiple posts/products by ID's through url

Hey guys,

I've been learning Laravel for the past couple of months. Now I'm a little bit stuck with the following. Searched all over the internet and can't find the fitting answer yet.

I'm trying to create a test e-commerce platform (just for fun). Now I'm trying to create a sidebar with filters. I already managed to get the categories to work.

Now I'm trying to have several checkboxes, which represent the brands. On click, I want to add this brand to the collection of products shown and show the brand(s) in the URL.

I've been trying a lot of things, if someone can point me in the right direction you'll be my hero!

0 likes
8 replies
mxbn's avatar
Level 1

Thanks for the answer but already got that working but how do I submit a checkbox, get the value, show the corresponding products & persist all the checkboxes checked.

Trying to recreate the typical e-commerce filter behavior.

Tray2's avatar

You should use an ajax request for this and handle the filtering in the background otherwise you have to append the status of each checkbox to your querystring.

mxbn's avatar
Level 1

How would you append the value to the query string? So it becomes something like:

?ids[]=1,2

Also how would you persist the checkbox to be checked after submitting?

Tray2's avatar

If you use ajax and do a submit in the background you just build the querystring

var queryString = 'checkbox1=' + document.querySelector('#checkbox1').value;

xhttp.open('GET', 'mysite.com/articles?' + queryString, true);

If you use a standard form submit you need to return the values by using ->withInput and then populate the checkboxes with it.

mxbn's avatar
Level 1

I will be using a normal form submit, no ajax involved. Can you elaborate how this would be done with a normal form submit? Trying a lot of things, but nothing seems to append the value to the URL.

Tray2's avatar
<form method="GET" action="/myurl">
    <input type="checkbox" name="checkbox1" value="{{old('checkbox1')}}">
    <input type="checkbox" name="checkbox2" value="{{old('checkbox1')}}">
    <input type="submit">
</form
mxbn's avatar
Level 1

I don't think this is working. I've got it working like this:

?brands[]=nike&brands[]=adidas

But I want a friendly looking URL for people to share like this:

?brands[]=nike,adidas
or even better
?ids=nike,adidas

I've got it working through the following form. Any suggestions on improving this?

                    {{ Form::open([ 'method' => 'GET', 'action' => array('SneakersPageController@index') ]) }} 
                      @foreach ($brands as $brand)
                        <p style="text-align:center;">
                            @if(in_array($brand->slug,$brandsArray) !== false)
                              <label for="brand">{{ $brand->name }}</label>
                              <input id="brand" name="brand[]" type="checkbox" value="{{ $brand->slug }}" onchange="this.form.submit()" checked> 
                            @else
                              <label for="brand">{{ $brand->name }}</label>
                              <input id="brand" name="brand[]" type="checkbox" value="{{ $brand->slug }}" onchange="this.form.submit()"> 
                            @endif
                        </p>
                      @endforeach 
                    {{ Form::close() }}

Please or to participate in this conversation.