最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery not posting all inputs of a form after the .append() - Stack Overflow

programmeradmin8浏览0评论

I have a form generated dynamically with the method .append() of jQuery. I can add any number of new input, textbox, cmbbox, etc...

But the problem is that when I do the sumbit of the form, the PHP target does not receive the new input added, but just the vars connected to the input already in the form before the append().

Any ideas?


The javascript:

$("#button").live('click',function add(){
   $("#list").append(
       '<li style="height:20px;">'
    +'<input type="text" class="text" id="prova" name="prova[]" value="prova">'+ 
       '</li>'
   );
});


The Html:

<input type="submit" id="button" value="Add input">
<form name = "form" id="form" action="post.php" method="POST">
   <ul style="width:670px;padding:0px 0px 30px 0px" id="list">
   </ul>
   <input type="submit" id="submit" value="Submit">
</form>


The PHP:

<?php
  print_r($_POST);
?>

I have a form generated dynamically with the method .append() of jQuery. I can add any number of new input, textbox, cmbbox, etc...

But the problem is that when I do the sumbit of the form, the PHP target does not receive the new input added, but just the vars connected to the input already in the form before the append().

Any ideas?


The javascript:

$("#button").live('click',function add(){
   $("#list").append(
       '<li style="height:20px;">'
    +'<input type="text" class="text" id="prova" name="prova[]" value="prova">'+ 
       '</li>'
   );
});


The Html:

<input type="submit" id="button" value="Add input">
<form name = "form" id="form" action="post.php" method="POST">
   <ul style="width:670px;padding:0px 0px 30px 0px" id="list">
   </ul>
   <input type="submit" id="submit" value="Submit">
</form>


The PHP:

<?php
  print_r($_POST);
?>
Share Improve this question edited Oct 3, 2010 at 0:44 Claudio asked Oct 2, 2010 at 20:53 ClaudioClaudio 1,2343 gold badges13 silver badges21 bronze badges 3
  • 2 The code work good, There was a problem in positioning of the tag <form> that was in a <div> and had to be outside – Claudio Commented Oct 3, 2010 at 1:07
  • 1 This comment is the key that led me to my repair. I had my <form> tag inside the <tbody> tag of the table to which I was .append() ing. Moved the <form> tag above the table and submit now includes the appended fields. – gordon Commented Jul 1, 2016 at 16:15
  • If the input is disabled, it will not be sent :) – mehmet Commented Dec 13, 2022 at 12:11
Add a comment  | 

5 Answers 5

Reset to default 5

Problem 1:

Your #button should not be of type submit, since you just want to use it to add to the form and not submit the form. So you should have:

<input type="button" id="button" value="Add input">


Problem 2:

You are overwriting your variables. The name is the variable sent with the form, so each input addition must have a new name, or the variable must be an array.

Additionally, you can't have more than one element with the same id.

The simplest way to solve this is to make prova an array by using the form prova[]:

$("#button").live('click',function() {
   $("#list").append(
       '<li style="height:20px;">' +
         // Removed repetitive ID and made prova an array
       '<input type="text" class="text" name="prova[]" value="prova"></li>'
   ); 
});

jsFiddle example

You are intercepting the click event and adding elements to the form, but the event has already started, and will complete its default action (submit the form) without re-checking the content of the form.

You should stop the event after adding the fields (preventDefault should be the right choice), and then re-submit the form.

Something along these lines:

$('#button').live('click', function add(event){
    event.preventDefault();
    $('#list').append(...);
    $('#form').submit();
});

I haven't tested it, but I'm pretty confident that it should work :)

Just to clarify, and putting any other problems aside, @Claudio's note is the correct answer here. I just had the same problem, button type was 'button' and the new element's name was being dynamically incremented. Everything looked fine, but the added elements would not submit.

Then I noticed my form tags were inside the table tags. I moved them outside and it all worked as planned.

Have any code to show? In order for php to "see" the vars submitted, you have to ensure that it has the "name" attribute specified on the form elements. I have a feeling your issue is going to be with the jQuery not the php.

Best guess: You haven't set name attributes for your dynamically added elements.

发布评论

评论列表(0)

  1. 暂无评论