1

I have this form:

<!DOCTYPE html>
<html>
    <body>
        <form action="draft.html?test=1">
            <button type="submit">Valid</button>
        </form>
    </body>
</html>

But when I click on valid it doesn't redirect to draft.html?test=1 but to draft.html. What am I doing wrong?

Thank you for your help.

EDIT: I know that I should favour <input type="hidden"> but I want to use it for a POST form.

EDIT 2: This is a stupid question, as soon as I change the method of the form to POST, the problem is resolved.

JacopoStanchi
  • 421
  • 5
  • 16

3 Answers3

6

If you absolutely need to put it on action attribute you can use method="POST".

<!DOCTYPE html>
<html>
    <body>
        <form action="draft.html?test=1" method="POST">
            <button type="submit">Valid</button>
        </form>
    </body>
</html>

Otherwise if you want to send your data trough GET use

<!DOCTYPE html>
<html>
    <body>
        <form action="draft.html" method="GET">
            <input type="hidden" name="test" value="1">
            <button type="submit">Valid</button>
        </form>
    </body>
</html>
Edgar Griñant
  • 599
  • 1
  • 12
  • 27
2

When you submit a form with method="GET" (the default), the query string of the action is replaced with the data in the form.

Don't put the data in the action attribute. Put it in hidden input elements instead.

<form action=draft.html>
    <input type=hidden name=test value=1>
    <button>Valid</button>
</form>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Is this what you want?

<form action="draft.html">
    <input type="hidden" name="test" value="1">
    <button type="submit">Valid</button>
</form>