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: [rentittoday] Long Url from Search Form

By Jason - May 18, 2010

Hi Lauren,

When using action=get, a form will always send a variable for each element in that form, regardless if it's filled in or not.

If post doesn't work for you, you could create a javascript function that will create a query string based on which fields are filled in:

Your form would look something like this:
<form method="get" action="?" onsubmit="submitForm(this);return false; />

Then, in your head tag, you would have a javascript function similar to this:

<script type="text/javascript">
function submitForm(form){
query="tester.php?";
if(form.input1.value!=""){query+="input1="+form.input1.value+"&";}
if(form.input2.value!=""){query+="input2="+form.input2.value+"&";}
if(form.input3.value!=""){query+="input3="+form.input3.value+"&";}

window.location=query;
}
</script>


This script builds a query string based on which fields are entered. Obviously you'll have to change names to match what's on your page.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

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