Remove unwanted variables from URL when using GET in an HTML form

Posted on 2011-09-14
Last Modified: 2012-05-12

I have an HTML form which is self populating from a database. The form is a series of dropdowns. The form is used to choose parameters for a search/filter function, The problem I have is that all variables are sent to the URL even when empty. For example:!

When really I need the URL to be:

How do I stop all these unwanted variables from being in the URL? There are a number of reasons why I want them removed.

Here is an example of the form I am using:

			<form method="get" action="scooters-list-test.php?"/>
            <select name = "model_range" > 
            	<option value = "">All Models</option>
				<?php foreach (getListOptions('products_scooters', 'model_range') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['model_range']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
            <select name = "engine_size"> 
				<option value = "">All Engine Types</option>
				<?php foreach (getListOptions('products_scooters', 'engine_size') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['engine_size']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
            <select name = "stroke_type"> 
				<option value = "">All Stroke types</option>
				<?php foreach (getListOptions('products_scooters', 'stroke_type') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['stroke_type']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
            <select name = "cooling_system" > 
				<option value = "">All Cooling Options</option>
				<?php foreach (getListOptions('products_scooters', 'cooling_system') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['cooling_system']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
            <select name = "colours" > 
				<option value = "">All Colour Options</option>
				<?php foreach (getListOptions('products_scooters', 'colours') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['colours']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
            <select name = "promotion_id_use" > 
				<option value = "">With / Without Promotion</option>
				<?php foreach (getListOptions('products_scooters', 'promotion_id_use') as $value => $label): ?> 
				<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['promotion_id_use']);?>><?php echo $label; ?></option> 
				<?php endforeach ?> 
           <input type="submit" name="submit" value="GO!" />

Open in new window

Question by:atari2600
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2

Expert Comment

ID: 36535312
just  replace the below line of code ..and it's work..
<form method="post" action="scooters-list-test.php?"/>

Open in new window

let me know your view.


Author Comment

ID: 36535337
Hi h4hardy,

Thanks, but I need the relevant variables to show. Otherwise bookmarking a search result will not be practical.

Expert Comment

ID: 36535378
no issue.. you can use the $_POST.. try the below code at your page where the form has been taken away after submitting the things..

in your case your action page is "scooters-list-test.php" so at there just type the below code..


echo "<pre>";


Open in new window

Note : don't forget to select any value from the  HTML page where the drop down is listed

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

LVL 82

Accepted Solution

leakim971 earned 500 total points
ID: 36535401
Set the onsubmit attribute of your form like this :
<form method="get" action="scooters-list-test.php?" onsubmit="return removeEmpty()">

function removeEmpty() {
     var f1 = document.getElementsByName("engine_size")[0];
     var f2 = document.getElementsByName("stroke_type")[0];
     var f3 = document.getElementsByName("cooling_system")[0];
     var f4 = document.getElementsByName("colours")[0];
     var f5 = document.getElementsByName("promotion_id_use")[0];
     if(f1.options[f1.selectedIndex].value.length == 0) f1.disabled = true;
     if(f2.options[f2.selectedIndex].value.length == 0) f2.disabled = true;
     if(f3.options[f3.selectedIndex].value.length == 0) f3.disabled = true;
     if(f4.options[f4.selectedIndex].value.length == 0) f4.disabled = true;
     if(f5.options[f5.selectedIndex].value.length == 0) f5.disabled = true;
     return true;

Open in new window


Author Closing Comment

ID: 36535487
Great solution! Exactly what I was looking for.

Author Comment

ID: 36535501
Out of curiosity how would I remove the:
at the end of the URL?

Author Comment

ID: 36535539
Actually just worked out how to do that :) Thanks again....
LVL 82

Expert Comment

ID: 36535624
>Out of curiosity how would I remove the:

Add this line : document.getElementsByName("submit')[0].disabled = true;

It's a bad practice to give a field the name "submit"
Change it to Submit or mysubmit


Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question