Welcome Guest, Not a member yet? Register   Sign In
change a view dynamically?
#1

[eluser]jorre[/eluser]
Hi all,

this is my first post here, but I have to admit that I already love CI.

Right now I have a beginner question:

I created a view with let's say 5 text fields in a form.
Now on this form I put a simple link link: <a href="add_more_fields">Add more fields</a>

When a user clicks this link, I want e.g. 5 text fields to be added into my form.
My question is: how can I accomplish this?

Should I create a function "add_more_fields" in the controller calling the view?
and then what?


Thanks already for your support, if CI has an active forum, I'm sure this won't be the last project I'm building on it!

Jorre
#2

[eluser]Armorfist[/eluser]
You can do this with javascript. After a quick search in google I came up with this site:

http://www.quirksmode.org/dom/domform.html

It will give you an idea how to accomplish what you want.
Hope it helps.
#3

[eluser]jorre[/eluser]
Thanks Armorfist. I know how to do this with Javascript...

the problem is that I have something like this in my controller:

$rules['poll_choice1'] = "trim|required";
$rules['poll_choice2'] = "trim";
$rules['poll_choice3'] = "trim";
$rules['poll_choice4'] = "trim";
$rules['poll_choice5'] = "trim";

So I want to do this for any input field on my view, also for the newly created input fields
#4

[eluser]sandwormusmc[/eluser]
Javascript is the way to go. Check out the appendChild functions available ... it's not too complex, plus JavaScript can be fun to learn. Tongue

edit: just noticed your last post. When you have the page POST back to your controller, check the length of your array and add the newly added form values to your array as well ...
#5

[eluser]Armorfist[/eluser]
To clarify sandwormusmc's post, you can do something like:

Code:
&lt;form name="form" method="post" action=""&gt;
  &lt;input name="textfield[]" type="text" /&gt;
  &lt;input name="textfield[]" type="text" /&gt;
  &lt;input name="textfield[]" type="text" /&gt;
  &lt;input name="textfield[]" type="text" /&gt;
  &lt;input name="textfield[]" type="text" /&gt;
  &lt;input type="submit" name="Submit" value="Submit" /&gt;
&lt;/form&gt;

When you post this and get the "$this->input->post('textfield')" variable, it will return an array:

Code:
var_dump($this->input->post('textfield'));

Returns:
array(5) { [0]=>  string(0) "" [1]=>  string(0) "" [2]=>  string(0) "" [3]=>  string(0) "" [4]=>  string(0) "" }

You can also define array indexes for the fields:

Code:
&lt;form name="form" method="post" action=""&gt;
  &lt;input name="textfield[f1]" type="text" /&gt;
  &lt;input name="textfield[f2]" type="text" /&gt;
  &lt;input name="textfield[f3]" type="text" /&gt;
  &lt;input name="textfield[f4]" type="text" /&gt;
  &lt;input name="textfield[f5]" type="text" /&gt;
  &lt;input type="submit" name="Submit" value="Submit" /&gt;
&lt;/form&gt;

Code:
var_dump($this->input->post('textfield'));

Returns:
array(5) { ["f1"]=>  string(0) "" ["f2"]=>  string(0) "" ["f3"]=>  string(0) "" ["f4"]=>  string(0) "" ["f5"]=>  string(0) "" }

Have fun!




Theme © iAndrew 2016 - Forum software by © MyBB