question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Form Submit - submit button doesn't get included in Form data

See original GitHub issue

I have an issue when attempting to submit a form where the value for the submit button itself isn’t sent. This is an issue because it is a website that I don’t control and is requiring that form data. Here is the important bits of the HTML of the form being submitted, along with the code I’m using:

HTML

<form action="/login" method="post" id="login">
 <fieldset>
    <dl>
      <dt><label for="username">Username:</label></dt>
      <dd><input type="text" name="username" id="username" /></dd>
    </dl>
    <dl>
      <dt><label for="password">Password:</label></dt>
      <dd><input type="password" id="password" name="password" /></dd>
    </dl>
    <dl>
      <dt>&nbsp;</dt>
      <dd><input type="submit" name="login" value="Login"></dd>
    </dl>
  </fieldset>
</form>

C#

var formData = new Dictionary<string, string>
{
  { "username", "Admin" },
  { "password", "password" }
};
var form = this.Document.QuerySelector<IHtmlFormElement>('#login');
var submitted = await form.SubmitAsync(formData);

I’ve narrowed the issue down to this line returning false. It looks like it is expecting the submit element to equal the form that the submit is inside, which doesn’t make sense to me: https://github.com/AngleSharp/AngleSharp/blob/9da30e34c2c5cffd814d22f0e122db6bda968dd5/src/AngleSharp/Html/InputTypes/SubmitInputType.cs#L21

I’m not sure what the intent of Boolean IsAppendingData(IHtmlElement submitter) is, but I pulled down your source and put it into my solution and modified the line to compare form-to-form rather than form-to-input, as so, which fixed the issue:

return Object.ReferenceEquals(submitter, Input.Form);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
LeastOnecommented, Nov 27, 2020

I’ve encountered this same problem. Having read the aforementioned distinction regarding the difference between a JavaScript form.submit() and a JavaScript button.click() I think one important detail is being overlooked which is that when JavaScript is disabled the browser does send the submit button name and value. Though AngleSharp doesn’t support JavaScript, it seems to me that API members such as IHtmlInputElement.DoClick() strongly suggest its mimicking non-JavaScript browser behavior. Regardless of what spec may be implemented, I think this practical reality provides good reason to modify AngleSharp’s behavior accordingly. Not that my anectodical situation should dictate the whole, but for me this prevented me from adopting AngleSharp in my most recent project. Do you think this could be revisted?

1reaction
BrunoJuchlicommented, Mar 30, 2023

I’ve run into this problem as well, but it looks like the workarounds are not necessary anymore since this is now possible out of the box:

IHtmlFormElement form;
var submitButton = form
    .Elements
    .OfType<IHtmlButtonElement>()
    .Single(b => b.Name.Is(buttonName));

 await form.SubmitAsync(submitButton);

Which results in the button name + value being part of the submitted data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FormData() object does not add submit-type inputs from ...
A button in the form is only included in the form data set if it's the submitter. But when this algorithm is executed...
Read more >
<input type="submit"> - HTML: HyperText Markup Language
A string indicating the HTTP method to use when submitting the form's data; this value overrides any method attribute given on the owning...
Read more >
Why can I not submit a form?
I have filled in all the data. But when I scroll to the bottom the submit button is greyed out and won't let...
Read more >
Javascript: prevent double form submission
Using JavaScript to disable form buttons and prevent double submission of an online form. Presenting a 'please wait' message while the form is...
Read more >
How can you create a form that doesn't have a submit button?
There are several ways to create a form that doesn't have a submit button, depending on the type of functionality you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found