I have a form where I need to dynamically add as many text fields as the user wants to. I want the text fields to be an array, for example:

<input type="text" name="room_text[]">

I've already built something I thought would work. It successfully adds more text boxes. I added two more text boxes with javascript dynamically, making the form look like this:

<input type="text" name="room_text[]">
      <input type="text" name="room_text[]">
      <input type="text" name="room_text[]">

BUT, when I posted it to the PHP file, it only gets the first value. The reason why I know this is a javascript problem is becuase it works fine if you have more than one text box on page load. Its just when you add more text boxes with javascript.

If this helps, this is the jquery function I use to add the boxes:

$('.add').live("click", function() {
    var mu = $(this).parent('td').parent('tr');
    var clone = $(this).parent('td').parent('tr').clone();
    $(clone).children('td').children('.add').remove();
    $(clone).children('td').children('.redtext').remove();
    $(clone).children('td').children('.remove').css("display", "inline");
    $(clone).css("display", "none");
    $(mu).after(clone);
    $(clone).show("fast");
});
share|improve this question
1  
You should check to see what the HTTP transaction looks like. You can use a Firefox plugin like Tamper Data to see the exact state of the outgoing HTTP request when you post the form. If the input elements are showing up on the page, then it's highly unlikely that this is a JavaScript problem. – Pointy Apr 4 at 14:01
2  
make sure the name attribute for the inputs room_text[] are being set properly ... if they're all the same then you will experience the behavior mentioned – Xander Apr 4 at 14:03
Could you just avoid the PHP parameter array-name '[]' processing altogether? Perhaps have the text inputs outside the form, then collect their values on-submit, collate their values into a 'hidden' input (inside the form) and post it to the server. – David-SkyMesh Apr 4 at 14:06
@Xander What exactly do you mean by making sure the attribute inputs are being set properly? – Phill Tidd Apr 4 at 17:31
the name attribute needs to be unique for each input text field... – Xander Apr 4 at 17:41
feedback

1 Answer

I believe the problem resides in the .clone() function. Can you try a different method, say ...

$('.add').live("click", function() {
    var mu = $(this).parent('td').parent('tr');
    var clone = '<tr>' + $(this).parent('td').parent('tr').html() + '</tr>';
    $(clone).children('td').children('.add').remove();
    $(clone).children('td').children('.redtext').remove();
    $(clone).children('td').children('.remove').css("display", "inline");
    $(clone).css("display", "none");
    $(mu).after(clone);
    $(clone).show("fast");
});

UPDATED - Oops. In that version "clone" is a string, not an element, so the .children() functions aren't working ... here's a corrected version:

$('.add').live("click", function() {
    var mu = $(this).parent('td').parent('tr');
    var clone = $('<tr>' + $(mu).html() + '</tr>');
    $(clone).children('td').children('.add').remove();
    $(clone).children('td').children('.redtext').remove();
    $(clone).children('td').children('.remove').css("display", "inline");
    $(clone).hide();
    $(mu).after(clone);
    $(clone).show("fast");
});​
share|improve this answer
There are known issues with older versions of IE and .clone() ... something about the "deep copy" functionality, I think. – Timothy Aaron Apr 4 at 14:17
I tried the code you send me, it still does the same thing, only reads first element – Phill Tidd Apr 4 at 17:29
Same code works for me -- timothyaaron.com/test.php. You sure it's not your PHP? – Timothy Aaron Apr 4 at 18:31
I don't think so, but possibly. I did a print_r of $_POST['room_text'] and just gives the first element – Phill Tidd Apr 4 at 19:23
Actually, anything I insert with javascript isnt getting picked up in PHP. Not just the arrays – Phill Tidd Apr 4 at 19:28
show 2 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.