user2502755 user2502755 - 1 year ago 61
HTML Question

Reload same page after form subit in HTML/PHP

I am using a get method to select options with years and months. The URL after submitting this selection looks as follows:

I can reload this page and the selection is still there. However, when i submit a form to add a record, the page reloads as follows:

And the selection is gone. How can I keep the seletion and reload the exact same url after submitting the form?

I am using the following code:

<form action="?" method="post">
<SELECT options with different inputs>
<input type="submit" value="Add">

In my PHP it looks the following:

header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);

It creates the right URL after submit, only the global variables are not updated. I defined those as follows:

$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');

And they are changed when I select an option.

Answer Source

You could traverse through submitted variables and add them to the HTML code.

<form action="target.php?var1=<?= $_GET["value2"]; ?>&var2=value2" method="get">

Or just use $_SERVER['REQUEST_URI'] inside action="".