Welcome Guest, Not a member yet? Register   Sign In
Post an array value from form
#1

[eluser]bikuta[/eluser]
Hi guys,

I'm trying to get an array of values from a form. e.g., I have a user edit form and a list of schools that user is attached to. The user part is simple as I can read the individual fields via $this->input->post(<field name>).

However I also need to store the array of schools (which is variable in length). At the moment I'm thinking of just using javascript to update a hidden field, but I was just wondering if there is an easier way to do this.

So preferably I could have one array called "schools" and so when I do $this->input->post("schools"). It will give me an array of all the schools. Is this possible?
#2

[eluser]bikuta[/eluser]
So could anyone give me pointers on what would be the best way to do this?
#3

[eluser]gtech[/eluser]
maybe your question is a tad unclear

how do you want these schools to be populated, are these

1)hardcoded?
2)retrieved from a database?
3)added manually by the user?

in the form does the user
select schools from a list?
are the schools selected from a database using the userid?

It really depends on how you using the schools array, and whether the user needs to select which schools are associated with them in the form and what you are doing with the information in the controller once the form is submitted as to what the best method is to achieve what you want.

if for example the schools is gathered from the database depending on the user, all you need to do is pass the userid though the form and then select the schools list from the database once the form has been submitted.
#4

[eluser]bikuta[/eluser]
Ok let me try to make it clearer:

At the moment I have a form which has the following fields:
- UserId
- UserFullname
- UserName
- Password
- Gender

Now this is fine, I can post the values normally using form post method.

Now I need to add another variable called "schools", which will be an array of Id's read from the database.
So I need a way for me to be able to post this array when the form is submitted.

So adding the school array means that the user can add/remove schools which are associated with the user.

The fields on the form would then look something like:
- UserId
- UserFullname
- UserName
- Password
- Gender
- Schools (this is an array)

How should I implement this Schools array, without changing how the form submission works?

In the backend I have 3 tables:
User table (UserId, UserFullname, UserName, Password...)
School table (SchoolId, SchoolName)
UserSchool table (UserId, SchoolId)
#5

[eluser]TheFuzzy0ne[/eluser]
[quote author="bikuta" date="1209208655"]Ok let me try to make it clearer:

At the moment I have a form which has the following fields:
- UserId
- UserFullname
- UserName
- Password
- Gender

Now this is fine, I can post the values normally using form post method.

Now I need to add another variable called "schools", which will be an array of Id's read from the database.
So I need a way for me to be able to post this array when the form is submitted.

So adding the school array means that the user can add/remove schools which are associated with the user.

The fields on the form would then look something like:
- UserId
- UserFullname
- UserName
- Password
- Gender
- Schools (this is an array)

How should I implement this Schools array, without changing how the form submission works?

In the backend I have 3 tables:
User table (UserId, UserFullname, UserName, Password...)
School table (SchoolId, SchoolName)
UserSchool table (UserId, SchoolId)[/quote]

I still don't get it... What interface is the user going to have at the front end with which to choose their school? How many schools can they choose?
#6

[eluser]gtech[/eluser]
use a check box or a "select multiple" these can be passed as an array
view:
Code:
&lt;?=form_open('home/parse');?&gt;
<select name="schools[]" multiple>
<option value="schoolid1" selected="selected">School 1</option>
<option value="schoolid2" selected="selected">School 2</option>
</select>
&lt;input type="submit"&gt;
&lt;/form&gt;
controller:
Code:
&lt;?php
class Home extends Controller   {
        function Home(){
             parent::Controller();
        }

        function parse(){
             print_r($_POST['schools']);
        }
        function formtest(){
             $this->load->view('formtest');
        }
}
?&gt;

example result:

Array ( [0] => schoolid1 [1] => schoolid2 )

note when using checkboxes the name of the variable in the from needs to have a [] after it so php knows its an array.
#7

[eluser]bikuta[/eluser]
Awesome thanks gtech, that's almost what I need.

TheFuzzyOne: the interface I'd like to have is a list where I will have a button to add and a button to delete (similar to what you would have in a todo list type of application).

So I wonder if I can use the name school[] for the list of school inputs...

i.e.,
Code:
&lt;input type="text" name="school[]" value="1" /&gt;
&lt;input type="text" name="school[]" value="2" /&gt;
&lt;input type="text" name="school[]" value="3" /&gt;
.
.
#8

[eluser]gtech[/eluser]
Code:
&lt;?=form_open('home/parse');?&gt;

&lt;input type="hidden" name="schools[0][schoolname]" value="school1"&gt;
&lt;input type="hidden" name="schools[0][data]" value="data"&gt;
&lt;input type="hidden" name="schools[1][schoolname]" value="school2"&gt;

&lt;input type="submit"&gt;
&lt;/form&gt;

you can test it with the controller I posted in the previous reply.

the schools post variable will display the following when using print_r:

Array ( [0] => Array ( [schoolname] => school1 [data] => data ) [1] => Array ( [schoolname] => school2 ) )


so maybee you can create some javascript to build up the hidden array when the users adds a school.
#9

[eluser]gtech[/eluser]
alternatively you can have two forms in your view, the second form is an add school form which will submit to a controller that adds the school to the database and then re displays the same view with the updated information.

to delete school have a delete icon next to the school name which is a href to a delete function.. the id is passed through the url and the controller method deletes (or marks for deletion) the school from the database and then redisplays the same view with the updated information.
#10

[eluser]bikuta[/eluser]
If I use two forms, does that mean the forms are nested or are they seperate?
How would the submit button trigger the second form post? Does I need to explicitly trigger the form to submit?




Theme © iAndrew 2016 - Forum software by © MyBB