Wiktoria Lewicka
Wiktoria Lewicka

Reputation: 81

Dynamic numbering of form field names

I have a script that duplicates the form like

<form name="formularz[form1]"> 
  <input type="text" name="formularz[form1] [imie]" value="<?php code ?>"/> 
  <input type="text" name="formularz[form1][nazwisko]" value="<?php code ?>"/> 
</form>

I also have a problem, because after duplicating the "form" I would like to change it to form2, form3, formN in the next forms.

How can I do this in jQuery? I have not shown the code which duplicates form elements because it is not relevant.

Upvotes: 1

Views: 45

Answers (2)

Supun Praneeth
Supun Praneeth

Reputation: 3160

You can try something like this:

var n = 100;//num forms you want

for (var i = 0; i <= n; i++) {

    var temp = '<form name="formularz[form' + i + ']"> \n' +
        '  <input type="text" name="formularz[form' + i + '] [imie]" value="<?php $code ?>"/> \n' +
        '  <input type="text" name="formularz[form' + i + '][nazwisko]" value="<?php $code ?>"/> \n' +
        '</form>';

    $('body').append(s);
}

Upvotes: 1

Sergo Kupreishvili
Sergo Kupreishvili

Reputation: 462

It is simple. use foreach like this:

<?php foreach($data as $key => $row): ?>
<form name="formularz[form<?=$key?>]"> 
  <input type="text" name="formularz[form<?=$key?>] [imie]" value="<?php code ?>"/> 
  <input type="text" name="formularz[form<?=$key?>][nazwisko]" value="<?php code ?>"/> 
</form>
<?php endofreach; ?>

Upvotes: 0

Related Questions