简体   繁体   中英

php(codeigniter) set default value with form helper

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

要为CI中的表单输入设置默认值,请使用set_value(),例如:

<?php echo form_input('txt_nama', set_value('txt_nama', ''));?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM