Welcome Guest, Not a member yet? Register   Sign In
CI 3 - Form helper and hidden fields. Am I doing this right?
#1

[eluser]talkingnews[/eluser]
I'm using PyroCMS which is built on CI 3 (so it says) and following this guide, most stuff seems to work. But I'm having a hard time figuring out the array to use for hidden fields. form_hidden doesn't work for anything requiring more than a basic name and value. And I HAVE to have an id field in there, so in the form_data it goes, with the type=hidden value.

I tried a two dimensional array first:

Let's build up a small array:

Code:
$trackdata = array();
          
        $trackdata[0]['type']='hidden';
        $trackdata[0]['id']='filename';
        $trackdata[0]['name']='filename';
        $trackdata[0]['value']='filename';
        
        $trackdata[1]['id']='track_name';
        $trackdata[1]['name']='track_name';
        $trackdata[1]['value']='trackname';

using
Code:
$theform = form_open('formtest2') . form_input($trackdata) . form_submit('submit', 'Update') . form_close();

gives me
Quote:<form action="http://test.co.uk/formtest2" method="post" accept-charset="utf-8">
<input type="text" name="" value="" 0="Array" 1="Array" />
<input type="submit" name="submit" value="Update" />
</form>

Whereas
Code:
$theform = form_open('formtest2') . form_input($trackdata[0]). form_input($trackdata[1]) . form_submit('submit', 'Update') . form_close();

gives me

Code:
<form action="http://test.co.uk/formtest2" method="post" accept-charset="utf-8">
<input type="hidden" name="filename" value="filename" id="filename"  />
<input type="text" name="track_name" value="trackname" id="track_name"  />
<input type="submit" name="submit" value="Update"  />
</form>

Well, the latter is more like it. But the only way I could get my multiple fields was like this:

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//dump();exit;
class Plugin_Formtest extends Plugin
{
    public function form_test()
    {
        $this->load->helper('form');
        
        $i = 1; // populate an array with some fake test data
        while ($i <= 5) {
            $hiddendata[] = array('type' => 'hidden', 'id' => $i, 'name' => 'name ' . $i, 'value' => 'track_name' . $i);
            $i++;
        }
        
        $theform = form_open('formtest2');
              
        //  recurse through and make multiple form_input's
        foreach ($hiddendata as $key => $value) {
            $theform .= form_input($hiddendata[$key]);
        }
        
       // and here's our text entry field
        $trackdata['id']    = 'track_name';
        $trackdata['name']  = 'track_name';
        $trackdata['value'] = 'track name';
        
        
        $theform .= form_input($trackdata) . form_submit('submit', 'Update') . form_close();
        
        echo $theform;
        return $theform;
        
        
    }
}

Took me a while to figure out, and it works fine, but I have this sneaking suspicion there's a better/tidier/more proper way to be doing this? And, no, posting the hidden array as the third parameter (
Code:
$hidden = array('username' => 'Joe', 'member_id' => '234');
echo form_open('email/send', '', $hidden);
) again won't work, because it's not the right kind of array?
#2

[eluser]TWP Marketing[/eluser]
[quote author="talkingnews" date="1347728822"]
Took me a while to figure out, and it works fine, but I have this sneaking suspicion there's a better/tidier/more proper way to be doing this? And, no, posting the hidden array as the third parameter (
Code:
$hidden = array('username' => 'Joe', 'member_id' => '234');
echo form_open('email/send', '', $hidden);
) again won't work, because it's not the right kind of array? [/quote]

This works fine for me under V 2.1.2 and I don't think it will be different under V 3.0 (not yet released)

When you say "it doesn't work", do you mean the hidden vars are not output as source code?
#3

[eluser]talkingnews[/eluser]
What I meant was that if I build an array of $hidden and put it in
Code:
form_open('email/send', '', $hidden)
, then it does this:

Code:
&lt;input type="hidden" name="0[type]" value="hidden" /&gt;
&lt;input type="hidden" name="0[id]" value="1" /&gt;
&lt;input type="hidden" name="0[name]" value="name 1" /&gt;
&lt;input type="hidden" name="0[value]" value="track_name1" /&gt;
&lt;input type="hidden" name="1[type]" value="hidden" /&gt;

and if I put the array in form_hidden, then it produces something along the lines of
Code:
&lt;input type="hidden" name="name1" value="hidden" id="name1" name="name2" value="hidden" id="name2" name="name2" value="hidden" id="name2" name="name2" value="hidden" id="name2"/&gt;

It's quite possible (though unlikely!) that I am doing in the correct way in my first post, it just seems a bit odd and unnecessary the way hidden fields are handled differently to input fields, when essential the only difference should be the "type=hidden" part.
#4

[eluser]TWP Marketing[/eluser]
[quote author="talkingnews" date="1347741773"]What I meant was that if I build an array of $hidden and put it in
Code:
form_open('email/send', '', $hidden)
, then it does this:

Code:
&lt;input type="hidden" name="0[type]" value="hidden" /&gt;
&lt;input type="hidden" name="0[id]" value="1" /&gt;
&lt;input type="hidden" name="0[name]" value="name 1" /&gt;
&lt;input type="hidden" name="0[value]" value="track_name1" /&gt;
&lt;input type="hidden" name="1[type]" value="hidden" /&gt;

and if I put the array in form_hidden, then it produces something along the lines of
Code:
&lt;input type="hidden" name="name1" value="hidden" id="name1" name="name2" value="hidden" id="name2" name="name2" value="hidden" id="name2" name="name2" value="hidden" id="name2"/&gt;

It's quite possible (though unlikely!) that I am doing in the correct way in my first post, it just seems a bit odd and unnecessary the way hidden fields are handled differently to input fields, when essential the only difference should be the "type=hidden" part.[/quote]

It's treating the two dimensional array as only one dimensional and creating a unique key for each value.
You could build that array as a one D matrix and retrieve it using a similar process.

Code:
$i = 0
foreach ( $record as $row ){
$hidden = array(
'id'.$i => $row['id'],
'name'.$i => $row['name'],
'value'.$i => $row['value'],
'type'.$i => $row['type'],
);
$i++;
}
$hidden['count'] = $i-1; // pass the actual array count so you can step through the POSTed values in the controller.
I think this is kind of a kludge to work around CI's limit on the hidden array processing




Theme © iAndrew 2016 - Forum software by © MyBB