0

I have a form that will be used for different action(inserting and updating). I use isset function for updating purpose. This is the complete code :

<div id="form_daftar">
    <?php echo form_open($action) ?>
        <?php
            if($action === 'backend/pengurus/add')
                $pengurus['email'] = '';
        ?>
        <p id="input_nama">
            Nama : <?php echo form_input('txt_nama', isset($pengurus['nama'])?$pengurus['nama']:"");?>
        </p>
        <p id="input_email">
            Email : <?php echo form_input('txt_email', 
            isset($pengurus['email'])?$pengurus['email']:"");?>
        </p>
        <p id="input_password">
            Password : <?php echo form_password('txt_pengurus');?>
        </p>
        <p id="input_alamat">
            Alamat : <?php echo form_input('txt_alamat', isset($pengurus['alamat'])?$pengurus['alamat']:"");?>
        </p>
        <p id="input_tanggal_lahir">
            Tanggal Lahir : <input type="text" id="datepicker" name="datepicker"
            value="<?php echo isset($pengurus['tanggal_lahir'])?$pengurus['tanggal_lahir']:""?>"/>  
        </p>

        <?php 
            $action === 'backend/pengurus/add' ? $label = "Daftarkan Pengurus" : $label = "Update Data Pengurus";
            echo form_submit('btn_insert', $label);
        ?>
    <?php echo form_close(); ?>
</div>

However, i got a weird bug so the isset is not working correctly. So, i tried to add this piece of code (snippet from above) :

<?php
   if($action === 'backend/pengurus/add')
      $pengurus['email'] = '';
?>

However, the $pengurus['email'] is not recognized, although its recognized in the form below this code (look the complete code).

What should i do?

Any help is appreciated, please just ask me if you dont understand my question (English is not my native languange). Thanks:D

1
  • @IgorS. well i dont tell you the detail because it weeird (and hard to say). My form_input just have a default value (i dont know where that value from, i might have to copy the entire code here to fix that). So, i think i just have to unset the $pengurus's value if the $action is for inserting :D Commented May 15, 2013 at 9:37

2 Answers 2

3

for setting default values for form inputs in CI, you use set_value(), like:

<?php echo form_input('txt_nama', set_value('txt_nama', ''));?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help, but i also need the isset for updating purpose
1

It is simple do this

when you load you form for insertion you can add an empty array and pass it to form

$pengurus['name'] = '';
$pengurus['email'] = '';
$pengurus['blah'] = '';

$data['pengurus']   =   $pengurus;

$this->load->view('form',$pengurus);

And when you load form for update you can fill these

$pengurus['name']   =   $row->name;
$pengurus['email']  =   $row->email;
$pengurus['blah']   =   $row->blah;

$data['pengurus']   =   $pengurus;

$this->load->view('form',$pengurus);    

//$row is an object retrieved with model

Now in the form you can do this

echo form_input('name', set_value('name',$pengurus['name']));

set_value takes 2nd parameter for default value. the default value for insertion is '' and for updation is some name. When you submit form and it fails it will check for the value you provided before submitting if not found than it will display the default(for insertion '' and for updation row->name). Hope this helps

4 Comments

Thanks for your help. I tried this in my controller, but it still not working :( if ($this->form_validation->run() === FALSE) { $data['pengurus']['email'] = ''; $this->load->view('template/header_v'); $this->load->view('backend/pengurus_form_v', $data); }
@BlazeTama what is the error try putting $pangurus['email'] = '[email protected]'
Theres no error, the default value (which i dont know where from) is still there. I tried it, and the value is not changed at all @@
do you mean when you are loading form on failed validation the default value is being displayed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.