penu
penu

Reputation: 1040

Input submit name is not being posted

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>
<p>"newProject" field from submit input is not being posted</p>

The only thing visible by doing print_r($_POST) is Array([projectName] => "") but the form name "newProject" is not visible or even being sent.

Here is a fiddle demonstrating it: https://jsfiddle.net/bosLyn1w/

Why is this happening and how can I submit the name of the submit input?

Upvotes: 1

Views: 80

Answers (1)

NigSys Technologies
NigSys Technologies

Reputation: 141

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">  </script>
<script>
$(document).ready(function(){
$('form').on('click', 'input[type=submit]', function(e) {
$(this.form).data('clicked', this.name);      
$('.pVal').html($(this.form).serialize()+ ' <br> '+this.name+'=' +this.value);
console.log($(this.form).serialize());
return false;
});
});
</script>
<form method="post">
<div class="a">
<label>Name</label>
<input name="projectName3" type="text">
</div>
<div class="b">
<input name="newProject4" class="newProject" value="Save Project" type="submit"></div>  
</form>
<p class="pVal"> -- Please put the both fields value here ---</p>

Upvotes: 1

Related Questions