1

I have a form that needs to be dynamic, allowing the user to add extra outcomes, w/ however many measures they need associated with it, as depicted here: https://i.sstatic.net/EMcoa.png

Here is a sketch of the naming scheme: http://i55.tinypic.com/2s6rh4y.png enter image description here

Below is the code in some form of working order: http://jsfiddle.net/bkmorse/yNZcD/2/

What I am having a problem with is making the name of the outcome and measure fields correct.

I need the first outcome textarea to be named: outcome[1]. amd every extra outcome textarea should be outcome[2], outcome[3] and so on.

As for the measure textareas, I need them be named like this: measure[1][0], measure[1][1], measure[1][2] and so on.

The measure textarea's are named like that, because they will be ran thru a loop when inserted into the database, and the first index number will relate to the outcome.

So when the user adds more measure textareas, the index should increase, for example...

outcome[2], has measure textarea: measure[2][0], measure[2][1], then when they click the plug sign, it will create a new measure textarea, named measure[2][2].

Same thing goes for when clicking the plus sign next to outcome, so the next outcome textarea name would be outcome[2], click for another outcome it would be outcome[3] and so on.

The code works, but I just need to have someone look at it and revise it so it has the name scheme I want.

5
  • I need them be named like this: measure1[0], measure1, measure1[2] and so on Did you mean: I need them be named like this: measure1[0], measure1[1], measure1[2] and so on ? Commented Apr 21, 2011 at 19:52
  • 1
    I would highly recommend the name convention i suggested here stackoverflow.com/questions/5568625/… so that you don't have keep track of the measure id's it will make your job much easier Commented Apr 21, 2011 at 19:52
  • Oops, I should I wrapped them in the code thing, I need them named like: measure[1][0] measure[1][1], so on. Commented Apr 21, 2011 at 19:52
  • @mcgrailm, I do like that convention, didn't see it before. I just need help revising the html and javascript to implement that naming convention, any help is appreciated. Commented Apr 21, 2011 at 19:56
  • 1
    I'll try to work it up for you Commented Apr 21, 2011 at 19:57

2 Answers 2

1

ok so I revised allot of code here is the WORKING DEMO by making the measures children of the outcome it will save you parsing in the php side of things.

I took out the names in the template portion to prevent them from posting.

I copy the measure form one that's already close by and clear the value since there is no need to renumber everything since it will already be named correctly.

I would also strongly suggest you take out the br span and strong tags those things should be handled with css or label would be a good idea

here is the html

<form action="#" method="post">
    <div class="outcomegroup" id="template">
        <div class="col">
            <strong>Outcome #<span class="onum">1</span></strong>
            <img class="plus addoutcome" src="http://i54.tinypic.com/2zqzzo7.jpg" />
            <img class="minus removeoutcome" src="http://i53.tinypic.com/29apc8i.jpg" />
            <br />
            <textarea class="outcome"></textarea>
        </div>

        <div class="col">

            <div class="measure1">
                <textarea></textarea>
            </div>

            <div class="measure">
                <textarea></textarea>
                <img class="plus addmeasure" src="http://i54.tinypic.com/2zqzzo7.jpg"/>
                <img class="minus removemeasure" src="http://i53.tinypic.com/29apc8i.jpg"/>
            </div>

        </div>

    </div>

    <div id="outcome-measure-fields"></div>
    <input type="submit" value="submit" />
</form>

and the jquery here:

function renumber() {
    $('#outcome-measure-fields .outcomegroup').each(function(index, value) {
        $(this).find('.outcome').attr('name', 'outcomes[' + index + '][outcome]');
        $(this).find('.measure textarea').attr('name', 'outcomes[' + index + '][measure][]');
        $(this).find('.measure1 textarea').attr('name', 'outcomes[' + index + '][measure][]');
        $(this).find('.onum').text(index + 1);
    });

}

$('.addmeasure').live('click', function() {
    $(this).closest('.measure').clone().insertAfter($(this).closest('.measure')).val('').find('.minus').show();
});

$('.addoutcome').live('click', function() {
    $('#template').clone().removeAttr('id').insertAfter($(this).closest('.outcomegroup')).find('.minus').show();
    renumber();
});

$('.removeoutcome').live('click', function() {
    $(this).closest('.outcomegroup').remove();
    renumber()
});

$('.removemeasure').live('click', function() {
    $(this).closest('.measure').remove();
});

$('#template').clone().appendTo($('#outcome-measure-fields')).removeAttr('id');
renumber()

and of course your css

.outcomegroup {
    overflow: auto;  
}

#template {
    display: none;
}

.col {
    float: left;
    margin: 5px;  
    font-size: 20px;
    font-weight: bold;
}

.plus, .minus {
    width: 20px;
    margin: 2px;
    vertical-align: middle;
    position: relative;
    top: -3px;
}

.minus {
    display: none;   
}

let me know how it goes

Sign up to request clarification or add additional context in comments.

8 Comments

I think something was stripped in your post, from: "I would also strongly suggest you take out the and would be a good idea" - if so, could you add that back in? I'll try it out, thank you very much.
It is not posting the outcome textarea's, but it is posting the measure textarea's correctly, as seen here: cl.ly/6AcP
@brad I corrected the problem
I changed it to textarea class="outcome" and removed the extra class="outcome", but it still doesn't capture the outcome data: cl.ly/6AW3
@brad also not that i changed the big array to outcomes so that it made more sense
|
1

granted mcgrailm's solution is perfectly valid, may i suggesting using a framework called knockoutjs? it's designed for exactly what you're doing; see this example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.