1

another question again regarding codeigniter, here a few details:

view page:

<?php if (isset($error)){echo $error; } ?>
<form action="<?php echo site_url('mem_posting/post');>" method="post">  
<input type="text" name="fname">
some fields goes here...
</form>  

controller page(mem_posting):

public function post_form()  
{
$this->load->view('header');  
$this->load->view(form_page);
}

public function post()  
{  
    $post_data=array(  
    'mem_id'=>$this->input->post('mem_id'),  
    //other inputs... 
    )
    $this->load->model('member_model');  
    if ($this->member_model->check_member($post_data)===true)  
    {  

   //row exist  
   // **i would like to load the same page but 
   // **with error message "like already exist".  

    }else{  
         $this->member_model->inset_member($post_data);
    }
}  

model page:

public function insert_member($post_data=array())  
{  
 extract($post_data);
 $this->db->where('member_id', $member_id);
 $this->db->insert('membership', $post_data); 

}

public function check_member($post_data=array())  
{  
    extract($post_data);
    $this->db->select('mem_id');
    $this->db->where('mem_id', $mem_id);
    $query = $this->db->get('membership'); 

    if ($query->num_rows() > 0){
        return true;            
    }else{
         return false;  
    }
}  

as you can see the view page contains the form, now what i would like to achieve is to echo an error like 'already exist' so i dont have to code another $this->load->view('post_form') inside the if statement.

thank you in advance..

2 Answers 2

2

Do you mean just sending a variable to the view?

$data['error'] = "error message";
$this->load->view('some/view', $data);
Sign up to request clarification or add additional context in comments.

Comments

0

Well! you shoul run validation when your form is submited. So, imagine you have 2 fields submited. It would be like this:

$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
$this->form_validation->set_rules('mem_id', 'ID Member', 'required|trim|is_unique[yourtable.mem_id]|xss_clean');
    if ($this->form_validation->run()){
       // Do your cool stuff now because validation passed
    }else{
       // Whatever...validation fails, load your view.
    }

So in your view you shoul have somethin like this at top of the form:

<?php echo validation_errors(); ?>

is_unique[yourtable.mem_id] will validate if the input is unique in your DB. So if is really unique, OK.

2 Comments

hi john, what i want to validate is if a row exist, if a row exist i would like to load the same page that contains the form but with error message,
Hi again! Sorry for my english, is not my native language, but if I did not misunderstood you...you submit a form, and validates if there is no row with that data, for example avoid having 2 rows with same username (for example). Is so, the code i posted will help you, is_unique[table.field] check that, and if there is a row with that data it will show error automatically. So if an user submit a new username, it will pass and do whatever you want, but...if the username exists....BOOM!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.