Sven
Sven

Reputation: 1

Input fields value is empty after clone

So I'm having a problem with the jQuery Clone function. I have this div with form-elements which I want to add to my form when the user asks for it.

But when I append a cloned input field to the form the input fields value will stay empty. This causes my POST-variable to be unset.

Has anybody any experience with this stuff?

Thanks

EDIT: I tried not using the clone function and did only this code: https://dl.dropbox.com/u/6614378/code.png

The code above still gives no real value to my input fields.

Upvotes: 0

Views: 811

Answers (1)

Sibu
Sibu

Reputation: 4617

Check this DEMO

$(document).ready(function(){
        var num = 1;
        $('#button').click(function(){
            var toAppend = '<br><input type="text" value="" placeholder="put something in" name="product['+ num +'][name]">';
            $('#product').append(toAppend);
            num++;
        });

      $('#frmsub').click(function(){
        $('input[name^="product"]').each(function(){
          alert($(this).val());
        });
        });

    });

As you can see, it has nothing to do with value attribute, whatever value you input in the textbox that will be the value of textbox

Update 2) Getting the values in php

<form id="product" action="" method="post">
    <input type="text" name="product[0][name]" value="5">
    <input type="hidden" name="multtxt" value="1">
    <input type="submit" name='frmsub' id='frmsub' value='submit'>
</form>

<?php
if(isset($_POST['multtxt'])){

    $prod_cnt = $_POST['product'];
    foreach($prod_cnt as $prod)
    echo $prod['name'];


}
?>

Upvotes: 0

Related Questions