php: submit form to current page keeping current url variables

Joined
12 Feb 2006
Posts
17,605
Location
Surrey
got a small problem google doesn't seem to have an answer for

i have a page with a few variables in the url which are required, if they aren't there the user is redirected to another page.

on the page i also have a small form which filters out what is displayed. the trouble i'm having is getting the form to submit the information using the get method and add a & rather then ? to the current url variables.

i currently do a for loop to make a string of whats in the url. it's then getting the form to realise variables are already there so another ? isn't needed.

thanks.

hope that made sense, me getting rather sleepy
 
hide the current $_GET values in the form....

PHP:
<form action="" method="get">
<?php
foreach($_GET as $key => $value) {
    echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
//rest of form goes here
?>
</form>

now when the form gets submitted, all the old values plus the new values from the form will be in the querystring. i think that's what you want? :p
 
Last edited:
What marc suggested is a good idea, although I'm not exactly sure what you're trying to do.

If you want to check whether variables are already in the query string, try
PHP:
if(!isset($_GET['key'])) {
// key was not found in querystring
}
 
Try this..
PHP:
<form action="<?php echo htmlentities($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']); ?>" method="get">
may need to add the question mark, I haven't test it.
 
Back
Top Bottom