• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Foreach and button mayham

silence

Well-known member
#1
Alright so I have a foreach loop and I'm trying to increment it after clicking the button, however it won't work. Is there a non-custom JS solution to this or is this an underlying flaw in XenForo?
Fieldset:
PHP:
    <fieldset>
        <xen:if is="!{$auth_id}">
            <dl class="ctrlUnit inputBox">
                <dt><label for="ctrl_auth_id">{xen:phrase teamspeak_identity}</label></dt>
                <dd><input type="text" name="auth_id[]" value="None" id="ctrl_auth_id" class="textCtrl" /></dd>
            </dl>
        <xen:else />
            <xen:foreach loop="$auth_id" key="$key" value="$value" i="$i">
                <dl class="ctrlUnit inputBox">
                    <dt><label for="ctrl_auth_id">{xen:phrase teamspeak_change_identity} #{$i}</label></dt>
                    <dd><input type="text" name="auth_id[]" value="{$value}" id="ctrl_auth_id" class="textCtrl" /></dd>
                </dl>
            </xen:foreach>
   
            <input type="button" value="{xen:phrase teamspeak_add_identity}" class="button smallButton FieldAdder JsOnly" data-source="dl.inputBox" data-maxfields="{$visitor.permissions.teamspeak.teamspeakIdentityCount}" />
        </xen:if>
    </fieldset>
Screenshot:
 

Jeremy

Well-known member
#2
You will need JS if you want to increment something post load unless you submit the form, reload the page completely, and update it that way.
 

Jeremy

Well-known member
#4
What exactly are you attempting to increment? And it should really be handled per-required instance since it can be so varying.
 

silence

Well-known member
#5
What exactly are you attempting to increment? And it should really be handled per-required instance since it can be so varying.
You can add x amount of identity fields based on a permission setting (data-maxfields="{$visitor.permissions.teamspeak.teamspeakIdentityCount})
I'm using the same feature used when adding poll responses. The only thing I'm incrementing is $i and merely for cosmetic purposes. Submitting works just fine, so I would love for a way to do this just to make it look dandy.
 

Jeremy

Well-known member
#6
You would have to store the value in JavaScript:

Code:
<script> var iValue = 0;</script>
And in each of your loops:
Code:
<script>iValue = {$i};</script>
Then when you add one, iValue++ you'll have your new number. Use that when inserting your new field.
 

silence

Well-known member
#7
You would have to store the value in JavaScript:

Code:
<script> var iValue = 0;</script>
And in each of your loops:
Code:
<script>iValue = {$i};</script>
Then when you add one, iValue++ you'll have your new number. Use that when inserting your new field.
Where would I put it since It seems to add on click of the button :S
 

Jeremy

Well-known member
#8
Well, the two scripts I mentioned, the first one should be before your foreach and the second within it. Usage would occur wherever your JavaScript to add a field is.
 

silence

Well-known member
#14
ResponseError is just that, an error. Something is wrong if you are getting that and an actual display.
Well I'm trying to tell the user if they use a name that's already in the db. It's all passing to controllerpublic just fine, but what would be an easy way to disable it to the user?
 

silence

Well-known member
#18
I can't get this working!
This works just fine:
PHP:
<script type="text/javascript">var identity = 1; function increment() { identity++; };</script>
PHP:
    <fieldset>
        <xen:if is="!{$auth_id}">
            <dl class="ctrlUnit inputBox">
                <dt><label for="ctrl_auth_id">{xen:phrase teamspeak_identity}</label></dt>
                <dd><input type="text" name="auth_id[]" value="None" id="ctrl_auth_id" class="textCtrl" /></dd>
            </dl>
        <xen:else />
            <xen:foreach loop="$auth_id" key="$key" value="$value" i="$i">
                <dl class="ctrlUnit inputBox">
                    <dt><label for="ctrl_auth_id">{xen:phrase teamspeak_change_identity} #<script>document.write(identity); increment();</script></label></dt>
                    <dd><input type="text" name="auth_id[{$i}]" value="{$value}" id="ctrl_auth_id" class="textCtrl" /></dd>
                </dl>
            </xen:foreach>

            <input type="button" value="{xen:phrase teamspeak_add_identity}" class="button smallButton FieldAdder JsOnly" data-source="dl.inputBox" data-maxfields="{$visitor.permissions.teamspeak.teamspeakIdentityCount}" />
        </xen:if>
    </fieldset>
However it still won't increment. I tried hooking to the button and incrementing there but it doesn't wanna budge. I tried other functions like alert and they work just fine when executing the button but the damn value won't change! Is it caching the last result or something?
 

Jeremy

Well-known member
#19
FieldAdded (the class that triggers the addition) doesn't handle the increment, you will have to override that (or internally use it in a function) that adds your new identity number in.
 

silence

Well-known member
#20
FieldAdded (the class that triggers the addition) doesn't handle the increment, you will have to override that (or internally use it in a function) that adds your new identity number in.
:/ that's just nasty. You think it's worth it since I want to be able to show to the user which field doesn't match the criteria and this seems to be the only way to do it D: