MarkA2049
MarkA2049

Reputation: 47

Dynamic HTML form with jQuery - Then later editing the data

Well,

I've been trying to figure this out for quite a while now with absolutely no luck.

Right now, I have an HTML form with jQuery that can dynamically add or remove a charge. This information is then put into a php array and stored into a database.

What I want, is to be able to pull that array from a database, count the entries, and place them into the correct number of input boxes on an HTML form. I would still like jQuery to be used here, as I would like users to be able to add or remove charges in this 'edit' stage.

If it's confusing, please ask questions.

jQuery v

<script type="text/javascript">
    $(document).ready(function() {
        $('#btnAdd').click(function() {
            var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
            var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

            // create the new element via clone(), and manipulate it's ID using newNum value
            var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

            // manipulate the name/id values of the input inside the new element
            newElem.children(':first').attr('id', 'name' + newNum)

            // insert the new element after the last "duplicatable" input field
            $('#input' + num).after(newElem);
            $('input[name="name[]"]:last').val(null);

            // enable the "remove" button
            $('#btnDel').attr('disabled','');

        });

        $('#btnDel').click(function() {
            var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
            $('#input' + num).remove();     // remove the last element

            // enable the "add" button
            $('#btnAdd').attr('disabled','');

            // if only one element remains, disable the "remove" button
            if (num-1 == 1)
                $('#btnDel').attr('disabled','disabled');
        });

        $('#btnDel').attr('disabled','disabled');
    });
</script>

HTML v

<tr id="input1" style="margin-bottom:4px;" class="clonedInput">
        <td><span style="color:#00CD00;">Charge:</span></td>
        <td><input type="text" name="name[]" size="35"/></td>
      </tr>
      <tr>
        <td><input type="button" id="btnAdd" value="Add Another Charge" /></td>
        <td><input type="button" id="btnDel" value="Remove Charge" /></td>
      </tr>

Also, if you seen any improvements I can use in my code, please do suggest. I am fairly new to using jQuery, and I am wanting to learn as much as I can.

Upvotes: 1

Views: 246

Answers (1)

Ela Buwa
Ela Buwa

Reputation: 1704

While repopulating the products in jquery is possible, I would try to repopulate them using php within a tag. This is purely to simplify the development. Populating with jquery would require getting the elements in a json feed and based on that, you can possibly re-use the same code you have to re-populate but this time, with a value in the text box.

Upvotes: 1

Related Questions