How to: display the full $_GET in the current page URL on form submit

Hey all,

I have a form, when a user presses enter on a typed/chosen selection from a form input I then store that to an array, and so I can display this as tags below the form input.

What I’d like to do is improve the way a user can go back to a search and save a search, the best way to do this will be with the page URL using queries.

E.g. example.com/?title=engineer|sotware developer|java developer&qualification=bachelor

What I’m not sure how to do, is as the user stores each value in the array - then change the URL query instantly. Any ideas?

gentle bump!

Do you need to update the URL? Could you have a ‘save this query’ button, without redirecting, that either saves a copy of the direct URL to the DB for the user to access later or copy the url to the clipboard for the user to paste wherever they want?

Hey @bpj!

I do need to update the URL as the user is updating their search live, e.g adding or removing a parameter from one of the inputs.

But, you’ve hit the nail on the head - I plan on using the full URL string to store in the DB as a ‘saved’ search. Then when the URL loads, I take the params from the URL and apply those to the arrays, and the inputs to create the same search.

What I can’t figure out is how to append/add/substract terms from the inputs. I’m thinking I’ll need a ‘goto’ URL type event for each change using the browser component?

How are you saving what’s been selected so far? You mention an array but I assume that needs to be keyed so you can add/remove specifics.

i.e. in the array do you have ['software engineer', 'batchelor']
or {title: 'software engineer', qualification: 'batchelor'}

Also, do you need to have the option of multiple for the same parameter i.e. qualification=batchelor, master of universe

So for our simplest search, we have say 6 inputs.
For each input we have a named array component. Whenever a user types/selects an input value and stores it (via enter, or on click) then we store that individual value in the array. We then display the selections beneath the field input using that same array.

On submit of form we then pass the array values via a hidden form input to our backend which then runs the query via JSON to an external API.

We aren’t currently saving a search in anyway, and it’s a negative UX when a user moves to another page and then goes back on the browser - they lose all of their search. If we can apply the search parameters to the URL dynamically, then we can more easily save a users search, plus if they hit back the search will just regenerate. I can get this working when I manually write the URL - but I’m a little stuck on how to generate the URL dynamically

Could you post some code for the form? How are the fields named?

Hey @bpj sorry for the delay - got late here yesterday. Here is a chunk of the code for the form but obviously not the whole form as it’s quite large! Perhaps might be easier to take a look at the staging environment?

                        <div class="row gx-3 ms-0 me-0" id="currentTitle">
                            <div class="col shadow-sm ps-0 pe-0 bg-white text-start">
                                <div class="row ms-0 me-0 justify-content-between">
                                    <button id="card1_collapse_Btn_curr_title" class="btn btn-link text-dark fw-bold text-start w-75" type="button" aria-expanded="true" data-bs-toggle="collapse" data-bs-target="#card1_collapsecurrtitle">Current
                                        Title</button><button id="card1_collapse_Btn_clear_curr_title1" class="btn btn-link text-muted btn-sm fw-light w-25" aria-expanded="true"
                                        dmx-on:click="arr_currentTitle.empty();contact_search_form.card1_collapse1.input_currentTitle.setValue('')" data-bs-target="#card1_collapse1">Clear</button>
                                </div>
                                <div>
                                    <div class="bg-white pt-0 pb-0 ps-0 pe-0" id="card1_heading7">
                                    </div>
                                    <div id="card1_collapsecurrtitle" class="collapse border-top rounded-0 border-light" is="dmx-bs5-collapse" aria-labelledby="card1_heading" data-bs-parent="" show="true">
                                        <div class="card-body bg-white" id="input_currentTitleCardBody1">
                                            <input id="input_currentTitle" name="input_currentTitle" type="text" class="form-control"
                                                dmx-on:keypress.enter="arr_currentTitle.addUniq(value);contact_search_form.card1_collapsecurrtitle.input_currentTitle.setValue('')" dmx-bind:value="query.cur_title">
                                            <input id="input_currentTitleh" name="input_currentTitleh" class="form-control" dmx-bind:value="arr_currentTitle.items" type="hidden">
                                            <div id="repeat_curr_t" is="dmx-repeat" dmx-bind:repeat="arr_currentTitle.items"><button id="btn1" class="btn btn-info btn-sm mt-2 me-1" dmx-text="$value+'&nbsp;x'" dmx-show="$value"
                                                    dmx-on:click="arr_currentTitle.remove($value)">Button</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row gx-3 mt-2 ms-0 me-0" id="location">
                            <div class="col shadow-sm ps-0 pe-0 bg-white">
                                <div class="row ms-0 me-0 justify-content-between"><button id="card1_collapse_location" class="btn btn-link fw-bold text-dark w-75 text-start" type="button" aria-expanded="true" aria-controls="collapse"
                                        data-bs-toggle="collapse" data-bs-target="#card1_collapseLocation">
                                        Location
                                    </button>
                                    <button id="card1_collapse_location1" class="btn btn-link text-muted btn-sm fw-light w-25" type="button" aria-expanded="true" aria-controls="collapse" dmx-on:click="arr_loc.empty();arr_latlong.empty()"
                                        data-bs-target="#card1_collapseLocation">Clear</button>
                                </div>
                                <div>
                                    <div class="bg-white pt-0 pb-0 ps-0 pe-0" id="card1_heading29">
                                    </div>
                                    <div id="card1_collapseLocation" class="collapse border-top rounded-0 border-light" is="dmx-bs5-collapse" aria-labelledby="card1_heading" data-bs-parent="" show="true">
                                        <div class="card-body bg-white">
                                            <div class="row">
                                                <div class="col align-self-center">
                                                    <p class="mb-0">Radius in miles</p>
                                                </div>
                                                <div class="col-5 ps-0">
                                                    <select id="selectRadius" class="form-control form-control-sm" name="selectRadius" dmx-on:changed="contact_search_form.submit(true)">
                                                        <option value="5">5</option>
                                                        <option value="20">20</option>
                                                        <option value="50">50</option>
                                                        <option value="100">100</option>
                                                        <option value="200">200</option>
                                                        <option value="500">500</option>
                                                        <option value="1000">1000</option>
                                                    </select>
                                                </div>
                                            </div>

                                            <input id="input_location" name="input_location" type="text" class="form-control" placeholder="Enter location" is="dmx-google-autocomplete" types="(regions)"
                                                dmx-on:updated="arr_loc.addUniq(value);contact_search_form.card1_collapseLocation.input_location.setValue('');arr_lat.addUniq(latitude);arr_long.addUniq(longitude);arr_latlong.addUniq(latitude+','+longitude)"
                                                style="margin-top: 6px;"><input id="input_locationh" name="input_locationh" type="hidden" class="form-control" placeholder="Enter location" dmx-bind:value="arr_loc.items">
                                            <input id="inp_latlong_hidden" name="inp_latlong_hidden" type="hidden" class="form-control" dmx-bind:value="arr_latlong.items.join(&quot;|&quot;)">
                                            <div id="repeat_curr_loc" is="dmx-repeat" dmx-bind:repeat="arr_loc.items"><button id="btn_r_loc" class="btn btn-info btn-sm mt-2 me-1" dmx-text="$value+'&nbsp;x'" dmx-show="$value"
                                                    dmx-on:click="arr_loc.removeAt($index);arr_latlong.removeAt($index)">Button</button></div>
                                            <input id="inp_radius_h" name="inp_radius_h" type="hidden" class="form-control" dmx-bind:value="selectRadius.value">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

Thanks for sharing that.
I would say that you could use the browser component but the goto parameter is going to be quite long, particularly for your most complex forms.

At the moment the fields are populating with query.curr_title for example. If you have multiple titles to search for, you have no way of reflecting that in the query string of the URL.
One option would be to change the inputs so the value is not set from the dmx-value, have as empty on load - use the removable tags to allow users to see what has been selected.
On page load use repeat through the query parameters with a flow and use.split(',') (will allow you to use a comma separated list for parameters that have multiple values) - add these to the relevant arrays using the array
Have the arrays reflected as ‘tags’ like you already have.

On enter, use the browser goto component and set up the URL:

example.com/?title={{arr_currtitle.items.join(',')}}&location={{arr_loc.items.join(,)}}...

You could change the comma separation to another character if your query parameters might contain commas.

1 Like

Thank you Ben, I think that url path will set me on my way now.

I actually managed to avoid using a repeat when I tested the url string function manually, by using this:

<dmx-array id="arr2" dmx-bind:items="query.cur_title.split('|')"></dmx-array>

So the url path would look like this example.com/?cur_title=engineer|developer

By using the split in the array, this then populates the tags and also the form input data for the form to be submitted.

Where I got lost was the URL path - I’m to test out your string, thank you!!

1 Like