Welcome Guest, Not a member yet? Register   Sign In
How to do an edit/update form?
#1

[eluser]lebe0024[/eluser]
Does anybody have any good examples?
#2

[eluser]Neovive[/eluser]
You may want to combine your add and update forms by passing an additional parameter either via a url segment or session that includes a specific ID. If the ID is present then retrieve the existing data from the database to edit, otherwise display a empty form to add.
#3

[eluser]Sarfaraz Momin[/eluser]
Cannot say that this is the best or the most optimized way of doing it but it gets the job done. I m providing the view and controller code for your reference

view.php
Code:
<?
$this->load->helper('form');
$this->load->helper('url');
?>
<?
if($action == "add"){
?>
<form name="addeditform" id="addeditform" method="post" action="<?= base_url()?>admin/category/add/save/">
<table cellpadding="0" cellspacing="0" border="0" width="98%">
<tr><td colspan="2" align="center"><h3>Add New Category</h3></td></tr>
&lt;?
}
elseif($action == "edit"){
?&gt;
&lt;form name="addeditform" id="addeditform" method="post" action="&lt;?= base_url()?&gt;admin/category/edit/save/"&gt;
<table cellpadding="0" cellspacing="0" border="0" width="98%">
<tr><td colspan="2" align="center"><h3>Update Category</h3></td></tr>
&lt;?
}
?&gt;
<tr><td colspan="2" align="center"><b><div id="msg"></div></b></td></tr>
<tr><td align="right" valign="top" width="50%">category name :</td><td>&lt;input name="category_name" value="&lt;? echo ($action == "edit")? $query[0]-&gt;category_name:"";?&gt;" style="width:200px;"></td></tr>
<tr style="padding-top:10px"><td align="right" valign="top" width="50%">metatags :</td><td>&lt;textarea name="metatags" style="width:250px;height:5em;"&gt;&lt;? echo ($action == "edit")? $query[0]->metatags:"";?&gt;&lt;/textarea&gt;</td></tr>
<tr style="padding-top:10px"><td align="right" valign="top" width="50%">template :</td>
<td>
<select name="template" id="template">
&lt;?
foreach($template as $tempitem):
echo "<option value=\"".$tempitem->template_id."\" ";
if($action == "edit"){
echo ($query[0]->template_id == $tempitem->template_id)? " selected":"";
}
echo ">".$tempitem->template_name."</option>";
endforeach;
?&gt;
</select>
&lt;input type="hidden" name="id" id="id" value="&lt;? echo ($action == "edit")? $query[0]-&gt;category_id:"add";?&gt;">
</td></tr>
<tr><td colspan="2" align="center" style="padding-top:10px">
&lt;input type="submit" name="submit" value="Save"&gt;
</td></tr>
</table>
&lt;/form&gt;


controller.php

Code:
if(!$this->uri->segment(3)){
            $data['query'] = $this->AdminModel->get_category();
            $this->load->view('adminviews/adminview_category', $data);
        }

        if($this->uri->segment(3) == "add"){ //add new category
            if($this->uri->segment(4) == "save"){
                $this->load->database();
                $data = array(
                'category_name' => $_POST["category_name"],
                'metatags' => $_POST["metatags"] ,
                'template_id' => $_POST["template"]
                );
                $this->db->insert('category', $data);
            }
            if($this->uri->segment(4) <> "save"){
                $data['template'] = $this->AdminModel->get_template();
                $data['action'] = "add";
                $this->load->view("adminviews/adminview_category_addedit",$data);
            }
        }

        if($this->uri->segment(3) == "edit"){ //update category

            if($this->uri->segment(4) == "save"){
                $this->load->database();
                $data = array(
                'category_name' => $_POST["category_name"],
                'metatags' => $_POST["metatags"] ,
                'template_id' => $_POST["template"]
                );
                $this->db->where('category_id', $_POST["id"]);
                $this->db->update('category', $data);
            }

            if($this->uri->segment(4) <> "save"){
                $data['query'] = $this->AdminModel->get_one_category($this->uri->segment(4));
                $data['template'] = $this->AdminModel->get_template();
                $data['action'] = "edit";
                $this->load->view("adminviews/adminview_category_addedit",$data);
            }
        }
#4

[eluser]section31[/eluser]
Your post doesn't do any type of datatype checking which will make it really easy to break.
#5

[eluser]enjoyfrancis[/eluser]
Thanks Sarfaraz Momin




Theme © iAndrew 2016 - Forum software by © MyBB