1

I am updating a field in my db table which is either empty or already have some text. If it is empty after update flash message should be like 'Your intro added' else like 'Your intro updated'.

Sample Table

This is my controller

function intro(){
$user_id = $this->session->userdata('user_id');
$table = 'users';
$this->load->model('my_model');
if($_POST){
date_to_save['intro'] = $this->input->post('intro');
$this->my_model->update($table, $user_id, $data_to_save);
$this->session->set_flashdata('success', 'Intro added');
//$this->session->set_flashdata('success', 'Intro updated');
redirect('Home');
}
else{
$data = array();
if($this->my_model->get($table, $user_id)){
$data['intro'] = $this->my_model->get($table, $user_id);
}
$this->load->view('intro',$data);
}
}

And here is my sample view

 <form>
 My Intro:<br>
 <input type="text" name="intro" value="<?php isset($_POST['intro'])? $_POST['intro'] :'';?>"><br>
 <input type="submit" id="submit" value="Submit">
 </form> 

I'm a beginner and looking for a simple solution.

Erin Smith
  • 11
  • 2

1 Answers1

2

There are different methods to achieve this, one is below

in view

 <input type="hidden" name="mode" value="<?php isset($_POST['intro'])? 'edit' :'add';?>">

and in your controller

if($_POST['mode']=='add'){
$this->session->set_flashdata('success', 'Intro added');
}
else{
$this->session->set_flashdata('success', 'Intro updated');
}
Mobeen Sarwar
  • 514
  • 5
  • 23