I want to pass more than one variable in the URL bar using PHP, however I want one of the variables to be added onto another existing variable in the URL bar.
For example:
Let's say you have: test.php?u=2 and let's suppose one of the fields is age. Once I click submit I want the URL to look like test.php?u=2&age=22 but on the same page.
How could I do this? Would I have to redirect the user again?
<form method="get" action="?">
- use this as your form tag. It'll submit to the same page, with all the form values in the querystring, accessible via $_GET
.
You can write code that injects all request variables into the form that the button submits. For example:
<form ... >
<?php foreach($_REQUEST as $key => $value) {
echo sprintf('<input type="hidden" name="%s" value="%s" />',
htmlspecialchars($key),
htmlspecialchars($value));
}
?>
<!-- the one you want to add follows -->
<input type="hidden" name="age" value="22" />
<input type="submit" />
</form>
It isn't pretty, but it works (if you don't mind that the vars from either $_GET
or $_POST
will actually end up being submitted with other method, whichever one it is the form uses).