Long Url from Search Form

4 posts by 3 authors in: Forums > CMS Builder
Last Post: December 10, 2010   (RSS)

By rentittoday - May 17, 2010

Hello,

We are having an issue with our urls when you search. The URL Search query is displaying all of the fields in the search, even if they are not filled out. See below:
"http://www.rentittoday.com/viewers/apartment_for_rent_results.php?num_match=&subcategory_match=&bedrooms_min=&bedrooms_max=&rental_price_min=&rental_price_max=&city_keyword=&surrounding_area_keyword=&state_keyword=&zip_match=&submit=Search"/

I know we can change the form action from "get" to "post" to not display the query string but we do want it to show the part of the query that the user filled out. So for example, if a user was searching Florida, it would say "......php?state_keyword=FL"
We are pretty sure that this was the case but they all of a sudden we started having these strings that go on forever!

How can we get back to our old prettier strings?

Thanks,
Lauren
Jason Glass

Re: [Jason] Long Url from Search Form

So it has been months and this functionality was seems to have be forgotten. But now it has been found and we are working on it again!

The code works wonderfully, thank you again!
But we have 1 minor issue that we would like to change.
The coding as it is now, adds an "&" after each searched term. I understand the need for this , and why it is setup the way it is but, we would like to only display the "&" ONLY if it is necessary.

First, here is our js code:

<script type="text/javascript">
function submitSearch(form){
query="viewers/motorcycle_results.php?";
if(form.category.value!=""){query+="category="+form.category.value+"&";}
if(form.subcategory_match.value!=""){query+="subcategory="+form.subcategory_match.value+"&";}
if(form.cities_serviced_keyword.value!=""){query+="cities_serviced="+form.cities_serviced_keyword.value+"&";}
if(form.state_match.value!=""){query+="state="+form.state_match.value;}

window.location=query;
}
</script>


If someone utilizes all of the search form elements while searching the end of the url is great :".../motorcycle_results.php?category=Dual%20Sport%20Bikes&subcategory=Big%20Dog&cities_serviced=Townsend&state=CO"

But if someone searches using just the category drop down (2nd search form element) it leaves an unnecessary "&" at the end:
/motorcycle_results.php?category=Dual%20Sport%20Bikes&

I removed the "&" from the 4th element (state_match) since that is the last thing a user could choose and will never need one. But the various ways that people could possibly search, and the fact that none of the search options are required has confused us.

Is there a way to go about adding the "&" only if it is necessary, (ie. because another form element has been selected after it)?

Thanks!
Jason Glass

Re: [rentittoday] Long Url from Search Form

By Chris - December 10, 2010

Hi rentittoday,

How about this approach?

<script type="text/javascript">
function submitSearch(form){
path = "viewers/motorcycle_results.php";
query = [];
if(form.category.value!=""){query.push("category="+form.category.value);}
if(form.subcategory_match.value!=""){query.push("subcategory="+form.subcategory_match.value);}
if(form.cities_serviced_keyword.value!=""){query.push("cities_serviced="+form.cities_serviced_keyword.value);}
if(form.state_match.value!=""){query.push("state="+form.state_match.value);}
var url = path + '?' + query.join('&');
window.location = url;
}
</script>


I hoep that helps! Please let me know if you have any questions.
All the best,
Chris