How to preserve GET parameters when posting form t

2019-06-15 07:40发布

问题:

I have a URL with one GET parameter. I am trying to post a simple form, basically to simply add one more GET parameter to the URL.

Current URL: mysite.com/page.php?first=123

Form HTML:

<?php $first = $_GET['first']; ?>

<form method="get" action="page.php?first=<?php echo $first; ?>">
<input type="text" name="second"><br>
<input type="submit" value="Submit"><br>
</form>

I'm trying to get the URL to be: mysite.com/page.php?first=123&second=456

However, when submitting the form, the page URL drops the first GET parameter and changes to: mysite.com/page.php?second=456

How can I submit this form and add the second GET parameter to add onto the end of the URL after the first already existing GET parameter?

Thanks

回答1:

You need to use hidden input instead:

<input type="hidden" name="first" value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />