CodeIgniter Forums
add and edit in one view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: add and edit in one view (/thread-40260.html)



add and edit in one view - El Forum - 04-04-2011

[eluser]mardon[/eluser]
Hi, I want add and edit records in db in one view
Code:
<div id="addcert_form">
    &lt;?php
    echo $akce;
    if ($akce == 'add') {
        echo form_open('admin/add_cert');
    }
    else {
        echo form_open('admin/edit_cert/'.$id);
    }
    echo $ser_cislo;
    ?&gt;
    <ul>
        <li>
            <label>Sériové číslo:</label>
            <div>
&lt;?php echo form_input(array('id' => 'ser_cislo', 'name' => 'ser_cislo', 'value'=>$ser_cislo)); ?&gt;
            </div>
        </li>
        <li>
            <label>Počátek platnosti:</label>
            <div>
&lt;?php echo form_input(array('id' => 'pocatek', 'name' => 'pocatek','value'=>$pocatek)); ?&gt;
            </div>
        </li>
        <li>
            <label>Konecplatnosti:</label>
            <div>
&lt;?php echo form_input(array('id' => 'konec', 'name' => 'konec','value'=>$konec)); ?&gt;
            </div>
        </li>
        <li>
            <label>Uživatel:</label>
            <div>
&lt;?php echo form_dropdown('id_user', $options, '1'); ?&gt;
            </div>
        </li>
        <li>
            &lt;?php
            if ($this->session->flashdata('login_error')) {
                echo "Nesprávné přihlašovací údaje";
            }
            echo validation_errors();
            ?&gt;
        </li>
        <li>
&lt;?php
if ($akce == 'add')
echo form_submit(array('name' => 'submit'), 'Přidat');
else echo form_submit(array('name' => 'submit'), 'Uložit');?&gt;
        </li>
    </ul>

but I have problem with value in input form when I add a record the value isnt set.
Its bad idea to have one form for add and edit? How to solve my problem?


add and edit in one view - El Forum - 04-04-2011

[eluser]Piter[/eluser]
Hi
Better is:
Code:
&lt;?php echo form_input(array('id' => 'ser_cislo', 'name' => 'ser_cislo', 'value'=>set_value('ser_cislo', $ser_cislo))); ?&gt;
If you have added to the from_validation set_rules.

I think it's better to do two grained forms, mute the mess in the code and faster to catch potecjalne errors.


add and edit in one view - El Forum - 04-04-2011

[eluser]mardon[/eluser]
Hi I fix it from you code, but still I have error in line 8 which is
Code:
echo form_open('admin/edit_cert/'.$id);

Severity: Notice

Message: Undefined variable: id

even when

Code:
$akce = 'add'



add and edit in one view - El Forum - 04-05-2011

[eluser]Piter[/eluser]
You do not have the variable $id, not bestowed on it to view.