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?
