jason
jason

Reputation: 1631

post form data and show in server

I have a simple form, I just want to see which fields are sent to server. But in the server side, I only get the <input type="text"> field value. Why the server can't get the <select> and <input type="file" /> value?

HTML:

<form action="http://localhost:8100/" method="POST">
        <div>
            <select>
                <option value="op1" selected="selected">Option1</option>
                <option value="op2">Option2</option>
            </select>
        </div>

        <div>
            <input type="file" value="select a file"/>
        </div>

        <div>
            <label>Your Hero: </label><input type="text" name="hero" />
        </div>

        <input type="submit" value="submit" />
    </form>

Server:

var http = require("http");

http.createServer(function(req, res) {
        if (req.method == 'POST') {
            var body = '';
            req.on('data', function (data) {
                body += data;
                console.log(body);
            });
            req.on('end', function () {
                console.log("request data complete");
            });
            res.end("post request");
        } else {
            res.end("get request");
        }

    }).listen(8100);

Upvotes: 0

Views: 53

Answers (2)

Farid Nouri Neshat
Farid Nouri Neshat

Reputation: 30410

Your select and file input elements are missing name attribute. All form elements you wish to be submitted must have a unique name attribute. name attribute will the identifier for the value of element when the data has been submitted through POST and GET.

You can read about it in the specs in here: http://www.w3.org/TR/html5/forms.html#naming-form-controls:-the-name-attribute

Upvotes: 1

fyimn
fyimn

Reputation: 11

I think you just forgot the "name" attributes in this form elments. Add it and it should work. Cheers

Upvotes: 1

Related Questions