0

First of all, why this isn't a duplicate: I am asking about handling multiple versions of a submission using Node. I didn't find any post asking that.

Basing on this I built this:

<form method="post">
    <% for(let i = 0; i < websites.length; i++){ let website = websites[i]; %>
    <fieldset>
        <label for="website<%= i %>" class="col-sm-2">Website <%= i + 1 %></label>
        <input class="website" name="website<%= i %>" id="website<%= i %>" value="<%= website %>" type="text"/>
        <span style="color: red"><%= errorMsgs[i] %></span>
        <a href="/websites/edit/<%= i %>" class="btn btn-info">Edit</a>
        <a href="/websites/delete/<%= i %>" class="btn btn-danger confirmation hide">Delete</a>
        <div id="removeBtn" class="btn btn-danger confirmation">Remove</div>
    </fieldset>
    <% } %>
    <input type="submit" class="btn btn-primary col-sm-offset-2" value="Generate a report" name="generateReport"
           data-toggle="modal" data-target="#exampleModal">
    <input type="submit" class="btn btn-primary col-sm-offset-2" value="Save websites" name="saveWebsites">
    <!-- Modal -->
</form>

I am using EJS, but I don't think it matters in this case.

And this is my back end:

console.log(req.generateReport);
console.log(req.saveWebsites);
if(req.generateReport!==null){
    console.log('report');
    return 0;
}
else if(req.saveWebsites!==null){
    console.log('save');
    return 0;
}

I was trying to use the answer I put a link to earlier, but it turns out, that the req.saveWebsites and req.generateReport properties are both undefined... So how do I handle it with Node?

I do not want to use AJAX, because I need it to work with JS disabled.

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • I don't think you can do that with plain HTML. Having two buttons submit the form to different places – yBrodsky May 02 '18 at 15:56
  • I know it's possible in Java. Would Node be behind in this? – Alex Ironside May 02 '18 at 16:01
  • No, the problem is in the frontend. Either button will just submit the form. If you need an additional parameter, you have to use javascript to intercept the submit and add it. – yBrodsky May 02 '18 at 16:03
  • 1
    Another thing, req.generateReport will always be undefined. You will be sending everything in the body. `req.body` – yBrodsky May 02 '18 at 16:04
  • @alex3wielki How would you do this in Java? Do you mean JavaScript? – Sidney May 02 '18 at 16:17
  • @Sidney Java. I posted the link to the answer in my question. Here's the link https://stackoverflow.com/questions/11830351/multiple-submit-buttons-in-the-same-form-calling-different-servlets. Using `req.body` doesn't change anything. But good catch – Alex Ironside May 02 '18 at 19:21

1 Answers1

0

I ended up just making 2 versions of the form. One with AJAX, and one without it.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Alex Ironside
  • 4,658
  • 11
  • 59
  • 119