Using Arrays as Field Names |
[eluser]moleculezz[/eluser]
Hello, I have a small problem using arrays as field names. It doesn't seem to work. I checked the user guide and came up with the following method of doing it. Form validation Code: <input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>" size="50" /> When I submit the form. The fields are populated but with the word array. I also tried the following, but that doesn't populate at all. Code: <input type="text" name="options[<?php echo $num;?>]" value="<?php echo set_value('options['.$num.']'); ?>" size="8" /> I am using a for loop to display multiple input fields.
[eluser]pickupman[/eluser]
Can we see a little more of your code. This part seems to be correct. Also, make sure you have set a validation rule in your controller for this work. Code: $this->form_validation->set_rules('options[]','options', 'trim');
[eluser]moleculezz[/eluser]
Hi, Yes, I have exactly that in my controller. So here is a little bit more detail about the code. Code: foreach($ids as $id){ There are more form elements I am using which are being repopulated as it should. Also the checkbox that I have in the for loop is also repopulating as it should, the text input is what is not repopulating.
[eluser]pickupman[/eluser]
Just out of curiosity is it showing up correctly in your $_POST array. Put this in your controller Code: $this->output->enable_profiler(TRUE); Submit the form, and see how it is shown.
[eluser]moleculezz[/eluser]
Yes, It does output the information. I tried the code you specified. BTW, didn't know about that command. This is much nicer for debugging. I used to use print_r(). Thanks for the tip!! Code: Array
[eluser]pickupman[/eluser]
Guess, I never had tried this before. I tried this and it worked. Code: <?php set_value() returns an 0 indexed array of submitted values. If you set it to a variable, then in your loop it will iterate the values in the array.
[eluser]moleculezz[/eluser]
Hi.. It took me a while to get back, was kind of busy with exams. But yeah, I tried this code, but I get the following error: Code: Severity: Notice
[eluser]pickupman[/eluser]
Then change the i=0 to i=1. It looks like you are getting a 1 based indexed array (first array key is 1).
[eluser]moleculezz[/eluser]
Ok.. so it's working but not quite there..I think I know what the problem is though. It doesn't recognize the variable until I submit the form. So I get undefined variable error. Only when I submit and the set_value() passes the array to the variable it works. But when i first load the page, I immediately get undefinded variable error. I came with a solution which is not that nice, but it works. Code: value="<?php if(!empty($options)) echo $options[$n]; ?>"
[eluser]pickupman[/eluser]
set_value() does take a second parameter. You will probably need to pass an array with the same number of values as there are fields. Code: $options = set_value('options[]',array('','','','','')); |
Welcome Guest, Not a member yet? Register Sign In |