-1

I need to debug missing data in some from POSTed to the server and after reading lots of tutorials and following other examples about that aspect, I still can't find my problem. The use case is pretty simple in theory: Have one form with two submit buttons to trigger different implementation on the server side.

According to lots of tutorials and examples, the submit-buttons should send their name if they have a name-attribute, while values should not be sent at all. The naming thing seems to differ according server side programming languages, sometimes it's some_name and sometimes some_name[], but that doesn't make any difference for me currently.

My problem is that whatever HTML I create, inputs of type submit are never part of the POSTed data. OTOH, pretty much the same HTML as button works as expected: If the button is used to submit the form, its name and even value are part of the POSTed data. When other inputs are clicked to submit, no names of any submit-input are available in the data.

So, with the exact same form, reaching the exact same endpoint, using same browser etc., the following DOES NOT provide any hint to the clicked button in the POSTed data:

<input  type="submit"
        name="foobar input"
        value="foobar input"
        title="foobar input"
/>

While the following OTOH does:

<button type="submit"
        name="foobar button"
        value="foobar button"
        title="foobar button">
    foobar button
</button>

So, should the input work the same way like the button does in theory? Or is the HTML wrong and I'm not able to spot the error? Sending the form itself works in both cases, though. So the browser obviously knows about the submit-input and its purpose.

Or have something changed the last years in modern browsers and submit-inputs are not part of POSTed data at all anymore for some reason? I can't remember the need to have multiple submits on a form for years.

How does a minimal example using a submit-input sending its name look like and tested to work for you? And in which browser? I tested an up-to-date Chromium based Opera and IE 11 and neither did include submit names.

Thanks!

Thorsten Schöning
  • 3,501
  • 2
  • 25
  • 46
  • I would not have a space in the name – mplungjan Jun 26 '20 at 09:11
  • @mplungjan The [name attribute](https://www.w3.org/TR/html4/interact/forms.html#adef-name-INPUT) contains [CDATA](https://www.w3.org/TR/html4/types.html#type-cdata). It can be more or less anything you like. (You shouldn't include leading or tailing white space because user agents can ignore it, but white space in the middle is fine). – Simone Rossaini Jun 26 '20 at 09:15
  • ***I*** would never have a space in name. As I would never call anything "submit" – mplungjan Jun 26 '20 at 09:16
  • Oh so is just opinion-based :) – Simone Rossaini Jun 26 '20 at 09:17
  • 1
    Yes, and backed up with 100s of questions here at SO saying "Why does this not work as expected" – mplungjan Jun 26 '20 at 09:18
  • 2
    https://imgur.com/a/w19MwlZ — I can't reproduce the problem. – Quentin Jun 26 '20 at 09:25
  • @mplungjan Spaces in the `name` don't change anything, tested without those as well and it wouldn't explain why one works while the other doesn't. – Thorsten Schöning Jun 26 '20 at 09:26
  • We need to see more HTML. Is it wrapped in a form? do you have a form prettifier that ignores the input for some reason? – mplungjan Jun 26 '20 at 09:28
  • @ThorstenSchöning As you can see [here](https://plungjan.name/SO/submittest/testform.html) your input and button work the same – mplungjan Jun 26 '20 at 09:37
  • 1
    @Quentin Thanks, I thought far too complicated and didn't spot the obvious: After reducing my HTML further and further, JS showed as the root cause. There was an event handler simply disabling all `input` of type `submit` before actually sending the form and didn't address `button`. :-/ After removing that, thinks work as expected again. – Thorsten Schöning Jun 26 '20 at 09:48

1 Answers1

0

OPINION: I would personally NEVER use more than one word in the name of a submit button

FACT: If that word is "submit" or you have id="submit" then you will not be able to programmatically submit the form using .submit()

FACT if you have script that disables the form element, it will not be sent to the server

Conclusion

In my experience and according to documentation - If you have the following

<form> 
  ................... 
  <button type="submit" 
    name="whatever you want here but I would only use one name and NOT submit">Whatever</button>
</form> 

OR

<form> 
  ................... 
  <input type="submit" 
  name="whatever you want here but I would only use one name and NOT submit" value"Whatever">
</form> 

your server will receive the button as name=value in the POST array if method = post and in the GET if nothing or method=get AND in the REQUEST array in either case (assuming PHP)

TEST PAGE

<form method="post" action="testsubmit.php">

Did not work according to OP<br/>

But it actually DOES work if not disabled from elsewhere <br/>

<input  type="submit"
        name="foobar input"
        value="foobar input"
        title="foobar input"
/>

<hr/>

<input type="text" name="sentField"  value="This WILL be sent to the server" style="width:300px"/>

<hr/>

<input type="text" name="disField" disabled value="This will NOT be sent to the server" style="width:300px"/>

<hr/>

Does work

<button type="submit"
        name="foobar button"
        value="foobar button"
        title="foobar button">
    foobar button
</button>



</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Yes, but that's what the OP has and they say it doesn't work. – Quentin Jun 26 '20 at 09:26
  • We do not know that yet. Is that input wrapped in a form? – mplungjan Jun 26 '20 at 09:27
  • They said "If the button is used to submit the form" – Quentin Jun 26 '20 at 09:27
  • But that's exactly what does NOT work for me, it makes a difference if `button` or `input` is used. Did you actually try that and if so, using which browser? Or are you only saying how you THINK it should work? – Thorsten Schöning Jun 26 '20 at 09:28
  • I have used buttons and inputs since 1995 or so. What you describe ***should*** not happen in the example I gave. Can you reproduce this in a blank paage with only a form and the buttons? – mplungjan Jun 26 '20 at 09:29
  • I'll accept that answer, because things DO still work as expected and that's what I wanted to know. My problem were JS-based event handlers simply disabling `input`s before actually sending the form and disabled button are not part of POSTed data. – Thorsten Schöning Jun 26 '20 at 09:50
  • That is correct. And I should have copied my comment about plugins/frameworks into my answer – mplungjan Jun 26 '20 at 09:55