Solved

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

Posted on 2011-09-14
8
418 Views
Last Modified: 2012-05-12
Hi,

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:

http://www.example.co.uk/scooters-list-test.php?model_range=Speedfight&engine_size=&stroke_type=&cooling_system=&colours=&promotion_id_use=&submit=GO!

When really I need the URL to be:

http://www.example.co.uk/scooters-list-test.php?model_range=Speedfight

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>
            <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>
            <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>
            <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>
            <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>
            <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 ?> 
 			</select>   
            
           <input type="submit" name="submit" value="GO!" />

Open in new window


0
Comment
Question by:atari2600
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:h4hardy
Comment Utility
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.

     
0
 

Author Comment

by:atari2600
Comment Utility
Hi h4hardy,

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

Expert Comment

by:h4hardy
Comment Utility
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..


<?php

echo "<pre>";
print_r($_POST);

?>

Open in new window


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



0
 
LVL 82

Accepted Solution

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

<script>
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;
}
</script>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:atari2600
Comment Utility
Great solution! Exactly what I was looking for.
0
 

Author Comment

by:atari2600
Comment Utility
Out of curiosity how would I remove the:
&submit=GO!
at the end of the URL?
0
 

Author Comment

by:atari2600
Comment Utility
Actually just worked out how to do that :) Thanks again....
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>Out of curiosity how would I remove the:
>&submit=GO!

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

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 45
Responsive CSS question 2 37
uploading multiple image with php 14 14
Bootstrap input box width 2 11
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now