Search code examples
htmlformspostget

POST forms: How to put GET parameters in action attribute?


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.


Solution

  • 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>