EllisLab text mark
Advanced Search
     
CI 3 - Form helper and hidden fields. Am I doing this right?
Posted: 15 September 2012 01:07 PM   [ Ignore ]
Joined: 2011-03-24
17 posts

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:

$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

$theform form_open('formtest2') . form_input($trackdata) . form_submit('submit''Update') . form_close(); 


gives me

<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

$theform form_open('formtest2') . form_input($trackdata[0]). form_input($trackdata[1]) . form_submit('submit''Update') . form_close(); 

gives me

<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:

<?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 (

$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?

 
Posted: 15 September 2012 04:29 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
talkingnews - 15 September 2012 01:07 PM

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 (

$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?

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?

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!

 
Posted: 15 September 2012 04:42 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-03-24
17 posts

What I meant was that if I build an array of $hidden and put it in

form_open('email/send'''$hidden

, then it does this:

<input type="hidden" name="0[type]" value="hidden" />
<
input type="hidden" name="0[id]" value="1" />
<
input type="hidden" name="0[name]" value="name 1" />
<
input type="hidden" name="0[value]" value="track_name1" />
<
input type="hidden" name="1[type]" value="hidden" /> 

and if I put the array in form_hidden, then it produces something along the lines of

<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"/> 

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.

 
Posted: 15 September 2012 05:10 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-12-19
612 posts
talkingnews - 15 September 2012 04:42 PM

What I meant was that if I build an array of $hidden and put it in

form_open('email/send'''$hidden

, then it does this:

<input type="hidden" name="0[type]" value="hidden" />
<
input type="hidden" name="0[id]" value="1" />
<
input type="hidden" name="0[name]" value="name 1" />
<
input type="hidden" name="0[value]" value="track_name1" />
<
input type="hidden" name="1[type]" value="hidden" /> 

and if I put the array in form_hidden, then it produces something along the lines of

<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"/> 

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.

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.

$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

 Signature 

CI 2.1.3, Linux, LAMP. We also like Gold and Silver…
Blame the hammer, it is obviously the guilty party…
User Guide? Nobody told me there was a User Guide!