1

I have a form with one input field and submit button. I know the name of the button can be submitted but for some reason, it isn't being posted to the server.

$(document).ready(function(){

  $("form").submit(function(e) {
      e.preventDefault();
      console.log($(this).serialize());
      return false;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
<div class="a">
  <label>Name</label>
  <input name="projectName" type="text">
</div>
<div class="b">
  <input name="newProject" value="Save Project" type="submit"></div> 
</form>

output: projectName=abc Expected output: projectName=abc, newProject=Save Project

Why is the "newProject" name not being sent to the server?

penu
  • 968
  • 1
  • 9
  • 22
  • 1
    [`.serialize()`](https://api.jquery.com/serialize/): _"Note: Only "successful controls" are serialized to the string. **No submit button value is serialized since the form was not submitted using a button**."_ – Andreas Mar 30 '17 at 07:49
  • You could always use a hidden input with a value if you want to do it that way. – Leggy Mar 30 '17 at 07:55
  • So the only way is to use hidden input or query for the value before posting? – penu Mar 30 '17 at 08:01
  • 1
    see - http://stackoverflow.com/questions/9866459/ajax-post-serialize-does-not-include-button-name-and-value?answertab=active#tab-top – Rohit Suthar Mar 30 '17 at 08:07
  • Ah I see, thanks for explaining – penu Mar 30 '17 at 08:29

1 Answers1

0

You can add data on top:

var value = $("[name='newProject']").val();
var data = $(this).serializeArray();
data.push({submit: value});
Vaishal Patel
  • 399
  • 4
  • 24