• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prevent empty fields from showing up in the url on form submit?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, hopefully this is the correct forum.

My java web project uses jsp and struts, but this question i think is more html form related.

I have an web app that submits a form for searching products. there are tons of advanced search options besides just your normal criteria string. categories, price range, product name, model, etc etc.

As a result, our URL looks huge (much bigger than just this in the real app): http://site.com/search?model=&productName=my+product&range=&category=

Most of the time, just one of these are filled out. Is there a way to have the form not submit empty string for all of these unused form fields when submitting the form so my URLs look a bit cleaner?

Thanks!
Aaron
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you can loop through the form yourself and find the fields that have values and than set the url

window.location.href="myUrl?" + strValues;

Eric
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure why it's an issue, but if you'd rather the empty fields not get submitted you can write an onsubmit handler that disables all the fields that are empty.
 
Aaron Wilt
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried setting disabled="true" on a field then submitting it, but it still showed up in the url.

i thought perhaps writing a js method to spin through all the form elements onsubmit and then manually creating the URL might be the only way... hoping for another way though.

question: does it matter that these elements are hidden?
[ January 09, 2008: Message edited by: Aaron Wilt ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aaron Wilt:
i tried setting disabled="true" on a field then submitting it, but it still showed up in the url. ...



Check your syntax and make sure you aren't getting any Javascript exceptions. It should be working. If need be, test the idea in a smaller, simpler page, like this:
 
reply
    Bookmark Topic Watch Topic
  • New Topic