views:

107

answers:

5

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);
?>
A: 

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.

jsuggs
A: 

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

Thomas
OP's code sets the `name` attributes, but they're all identical.
Peter Ajtai
A: 

You can try

var_dump($_POST);

in php to see what is going on.

Mikk
+1  A: 

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 :)

MartinodF
Where i have to put the code? it's event.preventDefault();?
Claudio
I added the code
MartinodF
Nothing, maybe a codeigniter problem?
Claudio
Instead of using `preventDefault()`, it'd be much simpler to change the `type` of `#button` to `button` instead of `submit`. Also, this doesn't solve the multiple identical `name` s.... see my answer.
Peter Ajtai
Also, why do you have a `$('#form').submit();` as the last line? That'll submit the form the first time `#button` is clicked... making `#button` pretty useless.
Peter Ajtai
#button is outside the form, so it will submit nothing.
Dr.Molle
Peter and Molle you're both right, I totally misread the question. Apologies.
MartinodF
+2  A: 

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

Peter Ajtai
Same problem, it's like the DOM did not see the new items and the submit not send those variables.
Claudio
@Claudio - It works. Take a look at this example ==> http://jsfiddle.net/XAZD7/ (click `add input` a few times, then `submit` - change the values in the inputs too if you want)
Peter Ajtai
It works :) thank you, there was a problem in the tag <form> in the html (not in the posted code)
Claudio
@Claudio - You're welcome ;)
Peter Ajtai