Search code examples
javascriptnode.jsexpresshidden-field

Submit a variable as value of hidden field in Express.js


I am trying to pass on a few parameters for a GET request using hidden fields, but I can't seem to set the value to a parameter I am using in my code. I am using the following code in my route:

app.post('/upload',function(req, res){
    var fileName = "filename.extension";
    res.write('<br> <form id = "download" enctype = "multipart/form-data" action = "/download" method = "get" > ' +
        '<input type="hidden" name="fileName" value=""+fileName /> ' +
        '<input type="submit" value="Download file" name="submit"> ' +
        '</form>');
    //document.getElementById('fileName').value = filePath;
}

I wanted to try to alter the value of the hidden fields fileName and saveAs via getElementById, but this resulted in an error.

Does anyone have any suggestions on how to deal with this?

Thanks a lot in advance!


Solution

  • Your quotation pattern in concatenation is not correct

       app.post('/upload',function(req, res){
        var fileName = "filename.extension";
        res.write('<br> <form id = "download" enctype = "multipart/form-data" action = "/download" method = "get" > ' +
            '<input type="hidden" name="fileName" value="'+fileName+'" /> ' +
            '<input type="submit" value="Download file" name="submit"> ' +
            '</form>');
        //document.getElementById('fileName').value = filePath;
    }
    

    you can also use javascript concat() method if you have difficulty matching quotes