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

XF 2.0 Using an array $this->filter('school_interest', 'array');

AndrewSimm

Active member
#1
I have a forum that allows a staff member to enter player information. Part of that forum gets submitted to my player table and part to my school_of_interest table. The part that gets submitted to school_of_interest allows you to submit more than 1 entry per field and I use some javascript to dynamically create more input boxes. The problem is when I submit the form, it only takes the first entry.

PHP:
$school_interest = $this->filter('school_interest', 'array');
HTML:
<xf:select name="school_interest[]" placeholder="Select College">
<xf:option value=""></xf:option>
<xf:foreach loop="$colleges" value="$college">
<xf:option value="{$college.man_college_id}">{$college.manage_college_name}</xf:option>
</xf:foreach>
</xf:select>
Screen Shot 2017-11-25 at 11.52.13 AM.png
 

Mike

XenForo developer
Staff member
#3
I believe that should be fine based on what you've shown, though you may want to look at what's submitted to the server and what's actually in $_GET and $_POST directly (since we pull the input from PHP's super globals).
 
#8
Click on your request and go to the "headers" tab. What does it show under "request payload"?
I see this

Code:
3

------WebKitFormBoundary8Wm22sN4kgOoiabW

Content-Disposition: form-data; name="chk[]"



on

------WebKitFormBoundary8Wm22sN4kgOoiabW

Content-Disposition: form-data; name="school_interest[]"



9

------WebKitFormBoundary8Wm22sN4kgOoiabW

Content-Disposition: form-data; name="school_status[]"



Undecided

------WebKitFormBoundary8Wm22sN4kgOoiabW

Content-Disposition: form-data; name="school_recruiter[]"
 

Mike

XenForo developer
Staff member
#10
So do you only see one entry for each named input there? That would generally be the problem then and would seem to either indicate the issue is in JS (form element name generation or submission content generation). You may need to look at that code in more detail.
 
#11
So do you only see one entry for each named input there? That would generally be the problem then and would seem to either indicate the issue is in JS (form element name generation or submission content generation). You may need to look at that code in more detail.
In the header, I only see it once. When I view the HTML of the page I see each element and the appropriate name. I am only using js to create part of the form. I still submit the forum the traditional way.
 
#12
Here is the js, I use to add the rows.

JavaScript:
function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    if(rowCount < 15){                            // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        }
    }else{
         alert("Maximum Schools is 15.");
              
    }
}
 
#13
You may find this template code useful:
Code:
            <xf:if is="$remainingResponses > 0">
                <li data-xf-init="field-adder" data-remaining="{{ $remainingResponses - 1 }}">
                    <xf:textbox name="poll[new_responses][]" value=""
                        maxlength="{{ max_length('XF:PollResponse', 'response') }}"
                        placeholder="{{ phrase('poll_choice...') }}" />
                </li>
            </xf:if>
From the poll_macros template in the public space. Other examples include captcha_question_edit in the admin space.


Fillip
 
#15
The javascript adds the row and form element, it just post the first row. Is there any specific with js I need to know with XF? The code itself works in the sense that it adds the additional row.
Sorry, I don’t understand what you mean by this. You should replace your custom JS with what I listed above, in order to make sure your code is fully integrated into XF2.


Fillip