HTML Forms Question

jgbishop

Senior member
May 29, 2003
521
0
0
I have an HTML form that uses the GET method (values are passed through the URL). In my form, I have an area where a user must specify a value. They can either choose a preset value from a drop-down menu, or they can enter a value directly in an edit box (this allows the user to provide values that might not be in the drop-down menu).

The user cannot both select an item in the menu and provide one in the edit box (I use JavaScript to catch this case) - they must do either one or the other.

How can I prevent the "empty" field from being submitted through the URL? Is there a way I can strip out the unwanted field before the GET is actually posted?
 

Traire

Senior member
Feb 4, 2005
361
0
0
I would do this using the POST method rather than GET, and use a bit of logic (ASP/PHP/Java, etc) on the recieving end to ignore the blank/empty variable. Is there a reason you need the data variables to go through the URL?
 

nweaver

Diamond Member
Jan 21, 2001
6,813
1
0
I'm not a java/ASP/PHP guy, I do perl CGI. I would do an "if $variable = "" then $variable = $textboxvariable" type statement. That way if the dropdown is null (assign a default null value?) then assign it the typed in variable. If not, ignore the typed in variable. THen you are not trying to read both. This should simplify the server side scripting, as you only have to evaluate on variable.
 

jgbishop

Senior member
May 29, 2003
521
0
0
Traire: I'm going through the URL for 'bookmarkability' mostly, but also because I'm not worried about security/data limitations. I'm only operating on a small amount of data, plus it makes debugging easier.

nweaver: Thanks for the suggestion. I gave it a try and it works great.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: Traire
I would do this using the POST method rather than GET, and use a bit of logic (ASP/PHP/Java, etc) on the recieving end to ignore the blank/empty variable. Is there a reason you need the data variables to go through the URL?
post and get are essentially the same thing for almost any server side language so I don't see any point in switching. In j2ee I don't think you can even tell the difference without manually parsing the url.

Anyways, I like the idea of just passing the extra value and ignoring it on the server side, but if you must remove it then some more javascript is probably in order. When the user picks which type of entry they want to use, you could just remove the other one from the form (maybe a bit tricky). Alternatively, when the form is submitted, you could locate the two controls and remove the one that isn't used. Another variation would be to post (ok, so maybe changing it might help :p), build the url manually at submission and then change the forms target. I did that once where I looped through the form and just appended &foo=bar to the url for each and you'd just need a special case for those two.