0

I'm using node.js with Express. I have a page /blah?name=john which produces a page with a form. When you click the form submit button it passes the form data to a POST request. However I want part of the post request to be whatever the ?name=john data was from the GET request that produced the page. How can I forward the name variable along with the submitted form data to a POST request?

Kyle V.
  • 4,752
  • 9
  • 47
  • 81

2 Answers2

1

Dynamically add the query string to some hidden input. So that your form will look like:

<form action="/submit" method="post">
    <input type="text" placeholder="Some text here">
    <!-- Hidden input -->
    <input type="hidden" name="name" value="john">
    <input type="submit" value="submit">
</form>

You should generate on the server this hidden input depending on the GET parameters :)

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
0

During the GET request, set the ACTION on your form to "MyPostPage.htm" + query_string. When the form is posted via the submit button, it will use the form's ACTION attribute which has the added query string information from the initial GET request.

<HTML>
    <FORM ACTION="http://example.microsoft.com/sample.asp?name='fred'&age=27" METHOD="POST">
        Enter your name: <INPUT NAME="FName"><BR>
        Favorite Ice Cream Flavor:
        <SELECT NAME="Flavor">
            <OPTION VALUE="Chocolate">Chocolate
            <OPTION VALUE="Strawberry">Strawberry
            <OPTION VALUE="Vanilla" SELECTED>Vanilla
        </SELECT>
        <P><INPUT TYPE=SUBMIT>
    </FORM>
</HTML>
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • Exactly what Chris said. so you would do this in the form `
    `
    – Joshua Pack Apr 27 '12 at 01:35
  • This sounds promising except that right now the form's action is set to `action="/submit"`so would I want it to be `action="/submit+query_string"`? I didn't know you could use query strings like that with POST requests. – Kyle V. Apr 27 '12 at 03:05
  • You can't retrieve a POST and a GET at the same time. – Florian Margaine Apr 27 '12 at 10:33
  • @FlorianMargaine - Not sure what you mean, the METHOD says post. And you most certainly can POST a form to a URL that contains query string parameters. – Chris Gessler Apr 27 '12 at 10:59
  • Yes, but I don't think you can retrieve GET parameters when you're sending a POST request. – Florian Margaine Apr 27 '12 at 12:00
  • @FlorianMargaine - The GET request occurs first. At that time, the query string variables are retrieved server-side (or could be parsed client-side with JS) and appended to the ACTION of the form. The new form action is then set to the server in as a POST method which also contains the appended query string parameters. On the server, one would simply look for both query string params and form variables. – Chris Gessler Apr 27 '12 at 12:17
  • Yeah, you can look and parse the query string, but that's just ugly :/ – Florian Margaine Apr 27 '12 at 12:26
  • @FlorianMargaine - ugly?? There's a header variable called QUERY_STRING. And parsing out the query string client side isn't difficult at all: http://stackoverflow.com/questions/647259/javascript-query-string – Chris Gessler Apr 27 '12 at 12:56
  • I do it all the time. `sample.asp?name='fred'&age=27` would have 2 GET responses and ` – Joshua Pack Apr 27 '12 at 15:35