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

Foreach and button mayham

Discussion in 'XenForo Development Discussions' started by silence, Jul 11, 2013.

  1. silence

    silence Well-Known Member

    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_idkey="$keyvalue="$valuei="$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:
    [​IMG]
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  3. silence

    silence Well-Known Member

    UHGGHGGGGGGGGGGGGGGGGGGGGGGGGGGGGG feature request time!
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    What exactly are you attempting to increment? And it should really be handled per-required instance since it can be so varying.
     
  5. silence

    silence Well-Known Member

    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.
     
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  7. silence

    silence Well-Known Member

    Where would I put it since It seems to add on click of the button :S
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    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 likes this.
  9. silence

    silence Well-Known Member

    It's XF default but should I hook into the onbuttonpress or w/e function?
     
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    Yes, you would want to attach something to .on('click')
     
    silence likes this.
  11. silence

    silence Well-Known Member

    Hmmm I gotta learn JS now :p well thanks for the push in the right direction mate :D
     
  12. silence

    silence Well-Known Member

    One more question, in return $this->responseError(), how can I break lines as to include multiple variables? Or should I be using something else?
     
  13. Jeremy

    Jeremy XenForo Moderator Staff Member

    ResponseError is just that, an error. Something is wrong if you are getting that and an actual display.
     
  14. silence

    silence Well-Known Member

    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?
     
  15. Jeremy

    Jeremy XenForo Moderator Staff Member

    Easiest would be to create an error view (template) and render it normally.
     
  16. LennardF1989

    LennardF1989 Member

    On a slightly unrelated note, I really love the template in that screenshot.
     
  17. silence

    silence Well-Known Member

    cyberap.net it's the style called 'Charm'!
     
  18. silence

    silence Well-Known Member

    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_idkey="$keyvalue="$valuei="$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?
     
  19. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  20. silence

    silence Well-Known Member

    :/ 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:
     

Share This Page