Welcome Guest, Not a member yet? Register   Sign In
HI guys how can i use same form for edit and add data into database?
#1

[eluser]Waqar[/eluser]
I want to use one form for both edit and delete data into database but i am not understanding how I can do that?
#2

[eluser]pistolPete[/eluser]
Insert and update in the same view
#3

[eluser]Waqar[/eluser]
Yeah sorry about the mistake i want to insert and update in the same view
#4

[eluser]umefarooq[/eluser]
Hello waqar
your add and edit are two different function of your controller, if you are using form helper for your form development in add function leave blank value section, while in edit function put your fatched records value

add function
Code:
$data['title'] = array('name'=>'name','id'=>'name');
$this->load->view('form',$data);

edit function

Code:
$row = fetch record from model;
$data['title'] = array('name'=>'name','id'=>'name','value'=>$row->title);
$this->load->view('form',$data);

and your view

Code:
<?=form_input($title)?>

try this it will work
#5

[eluser]n0xie[/eluser]
[quote author="pistolPete" date="1239129694"]Insert and update in the same view[/quote]
Click there. You will find http://codeigniter.com/wiki/Add_Edit_Views/
#6

[eluser]Waqar[/eluser]
Here is my controler

Code:
function add_edit()
    {
        $data['query']= $this->tpc_model->add_individual();
        redirect('individual/index');
    }

model
Code:
function add_individual()
    {
    
        $data = array(
        'title'=>$this->input->post('title'),
        'firstname' =>$this->input->post('firstname'),
        'surname' =>$this->input->post('surname'),
        'initials' =>$this->input->post('initial'),);
              
               if($id = $this->input->post('id'))
        {
            $where = "id = '$id'";
            $query = $this->db->update('individuals', $data, $where);
        }
        else
        {
            $query = $this->db->insert('individuals', $data);
        }
my view
Code:
$attributes = array('class' => 'indvidual', 'id' => 'individualform');
echo form_open('individual/add',$attributes);?>

<?php form_hidden('id',$id);?>
        <label for="title">Title: </label>
        &lt;?php $options = array(
                  'Mr'  => 'Mr',
                  'Mrs'    => 'Mrs',
                  'Ms'   => 'Ms',
                  'Dr' => 'Dr',
              'Prof' => 'Prof',
                );
                
        echo form_dropdown('title', $options);?&gt;<br>
    <label for="firstname">First Name: </label>
        &lt;input type="text" id="firstname" name="firstname" value="&lt;?=$firstname; ?&gt;"&gt;&lt;br>
        <label for="surname">Surname: </label>
        &lt;input type="text" id="surname" name="surname"&gt;&lt;br>
        <label for="initial">Initials: </label>
        &lt;input type="text" id="initial" name="initial"&gt;&lt;br>
        &lt;input type="submit" id="go" value="Save"&gt;&nbsp;
        &lt;?= form_close();?&gt;
#7

[eluser]Waqar[/eluser]
I am not understanding how to use the values in the form field if id is present coz it is giving me severity error.
#8

[eluser]umefarooq[/eluser]
hi nothing is difficult now u have to modify little things in code and in you database table there should be one field name id or what ever you like as a key to update your record and it can be your auto increment key in your table also


add function
Code:
$data['title'] = array('name'=>'name','id'=>'name');
$data['id'] = array('id'=>'');
$this->load->view('form',$data);

edit function

Code:
$row = fetch record from model;
$data['title'] = array('name'=>'name','id'=>'name','value'=>$row->title);
$data['id'] = array('id'=>$row->id);
$this->load->view('form',$data);

and your view

Code:
&lt;?=form_input($title)?&gt;
&lt;?=form_hidden($id)?&gt;
get the id value in controller and make in hidden in you view and after submitting check if id has any value than update the record if there is not id than insert new record.
#9

[eluser]Waqar[/eluser]
I am having problem loading the view form with values. it will load the edit form
but when i click on add it will give me severity error and undefined row
Quote:&lt;?php
if($this->uri->segment(3)){
$id=$this->uri->segment(3);
$this->load->database();
$query= $this->db->query("SELECT * FROM `individuals` WHERE id='$id'");
foreach($query->result() as $row){
}

}
?&gt;
&lt;input type="hidden" name="id" value="&lt;?=$row-&gt;id?&gt;" />
<label for="firstname">First Name: </label>
&lt;input type="text" id="firstname" name="firstname" value="&lt;?=$row-&gt;firstname?&gt;"><br>
<label for="surname">Surname: </label>
&lt;input type="text" id="surname" name="surname" value="&lt;?=$row-&gt;surname?&gt;"><br>
<label for="initial">Initials: </label>
&lt;input type="text" id="initial" name="initial" value="&lt;?=$row-&gt;initials?&gt;"><br>

help would be appreciated;
#10

[eluser]umefarooq[/eluser]
hi you are no following the MVC pattern that's why you are getting this error your code should be in you controller, that' you are getting error here is your code will be like this

Model
Code:
function get_record($id){
  $query= $this->db->query(“SELECT * FROM `individuals` WHERE id=’$id’”);
   return $query->row();
}


controller
Code:
if($this->uri->segment(3)){
  $id=$this->uri->segment(3);

     $this->load->model('model_name');
      $data['row'] = $this->model_name->get_record($id);
      $this->load->view('view_name',$data);
  }

View

Code:
&lt;input type=“hidden” name=“id” value=”&lt;?=$row-&gt;id?&gt;” >
<label for=“firstname”>First Name: </label>
      &lt;input type=“text” id=“firstname” name=“firstname” value=”&lt;?=$row-&gt;firstname?&gt;”>

      <label for=“surname”>Surname: </label>
      &lt;input type=“text” id=“surname” name=“surname” value=”&lt;?=$row-&gt;surname?&gt;”>

      <label for=“initial”>Initials: </label>
      &lt;input type=“text” id=“initial” name=“initial” value=”&lt;?=$row-&gt;initials?&gt;”>

you code will be clean




Theme © iAndrew 2016 - Forum software by © MyBB