Repopulating hidden fields with CodeIgniter Form Validation class

2.1k views Asked by At

I am stuck how to repopulate a hidden field in a form after an unsuccessful form validation.

The repopulation uses set_value() function to print out the chosen value for other fields, but with the hidden field, there is already something printed in the value field.

Here is my view code:

<? echo validation_errors();?>
<? echo form_open('projects/start');?>
<input type="hidden" name="project_type_id" value="<? echo $this->uri->segment(3);?>" >
<input type="text" name="site" value="<?echo set_value('site');?>">
<input type="submit" value="submit">
<?echo form_close();?>
2

There are 2 answers

0
Madan Sapkota On BEST ANSWER

Pass the URI segment value in second parameter, after submit the form it will take the submitted value, It repopulate submitted value not URI segment value.

<?php echo validation_errors(); ?>
<?php echo form_open('projects/start'); ?>
<input type="hidden" name="project_type_id" value="<?php echo set_value('project_type_id', $this->uri->segment(3)); ?>" />
<input type="text" name="site" value="<?php echo set_value('site'); ?>" />
<input type="submit" value="submit" />
<?php echo form_close(); ?>

Hope this helps you. Thank you.

0
danneth On

Taking a guess here that you want to fill it with the URI segment when it first loads, and that this is not available once the form submits.

Perhaps this would work

<input type="hidden" name="project_type_id" value="<? echo ($this->input->post())? set_value('project_type_id'): $this->uri->segment(3);?>" >

Depending on how you built your site it might also be possible to do

<? echo form_open('projects/start/' . $this->uri->segment(3));?>

To keep the URI