1

So I am validating a form using code igniter which simplifies my code a lot however I am facing a new problem. My form at first gets loaded as a pop up inside a view. But when the controller returns response after validation then the form opens as a separate webpage instead of just loading inside the previous view.

main_view.php

<script>
 function div_show(type, classID) {    
   if(type=='adduser')
   {
     document.getElementById('AddUser_popup').style.display = "block";
     $("#AddUser_popup").load("add_user");
   }
 }
</script>
<body>
  <button id="popupNewTerm" onClick="div_show('adduser', null)">Add user</button>
  <div class="AddUser_popup" id="AddUser_popup">  </div>
</body>

Controller:

public function add_user()
{
    $data = array();
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('user_m');
    $this->form_validation->set_rules('username','Username', 'required|trim');
    $this->form_validation->set_rules('emp_email','E-mail', 'required|trim|valid-email|xss_clean');

    if($this->form_validation->run()==FALSE)
    {
        $this->load->view('includes/forms/add_user', $data);
    }
    else {
        $data['username']=$this->input->post('username');
        $data['emp_email']=$this->input->post('emp_email');


        $user=array(
            'user_id'=> NULL,
            'username'=> $data['username'],
            'emp_email'=>$data['emp_email']
        );
        $this->user_m->insert_user($user);
        $this->load->view('includes/forms/add_user', $data);
    }
}

Form-> (add_user.php)

<div id="popupContact">
 <?php

 if(isset($username) && isset($emp_email))
 {
     echo validation_errors();
     echo 'User added successfully!';
 }
 else {
    echo validation_errors();
    echo form_open('', 'id="form" name="form"');
    echo '<p id="close" onclick ="div_hide()">X</p>';
    echo '<h2>Add User</h2>';
    echo '<hr>';
    echo '<label for="username">Username: </label>'.form_input('username', set_value('username')) .'&nbsp;&nbsp;';
    echo '<label for="emp_email">Email: </label>'.form_input('emp_email', set_value('emp_email')) . '<br><br>';


    echo form_submit('submit', 'Submit', 'id="submit"');

    echo form_close();
 }
 ?>
</div>

how can I load the form inside the main_view.php after the validation fails from the controller and when the validation is successful, I want the form to close inside the main page. I can do form validation using normal javascript and php but wanted to learn code igniter method of validation. Thanks.

1 Answer 1

1

If you have an issue in Validation regarding Codeigniter, then please read CodeIgniter Official Form Validation Guide

I recommend Saving Sets of Validation Rules to a Config File

If you are still confuse the read/review the piece of below code, it will help you. This code is used in Config File.

<?php

$config = array(
    'master/user' => array(
        array(
            'field' => 'empname',
            'label' => 'Employee Name',
            'rules' => 'required|trim|min_length[6]|xss_clean'
        ),
        array(
            'field' => 'cnt',
            'label' => 'Contact Number',
            'rules' => 'required'
        ),
        array(
            'field'=>'dob',
            'label'=>'Date Of Birth',
            'rules'=>'required'
        ),
        array(
            'field' => 'design',
            'label' => 'Designation',
            'rules' => 'required'
        ),
        array(
            'field'=>'pass',
            'label'=>'Password',
            'rules'=>'required'
        ),
        array(
            'field' => 'cpass',
            'label' => 'Password Confirmation',
            'rules' => 'required|trim|min_length[6]|matches[pass]|xss_clean'
        ),
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required'
        ),

//        array(
//            'field' => 'prof',
//            'label' => 'Profile Image',
//            'rules' => 'required|callback_upload_image'
//        )
    ),
    'master/SAdmin' => array(
        array(
            'field' => 'empname',
            'label' => 'Employee Name',
            'rules' => 'required|trim|min_length[6]|xss_clean'
        ),
        array(
            'field' => 'cnt',
            'label' => 'Contact Number',
            'rules' => 'required'
        ),
        array(
            'field'=>'dob',
            'label'=>'Date Of Birth',
            'rules'=>'required'
        ),
        array(
            'field' => 'design',
            'label' => 'Designation',
            'rules' => 'required'
        ),
        array(
            'field'=>'pass',
            'label'=>'Password',
            'rules'=>'required'
        ),
        array(
            'field' => 'cpass',
            'label' => 'Password Confirmation',
            'rules' => 'required|trim|min_length[6]|matches[pass]|xss_clean'
        ),
        array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'required'
        )

    ),
    'master/task' => array(
        array(
            'field' => 'jtitle',
            'label' => 'Job Title',
            'rules' => 'required|trim|xss_clean'
        ),
        array(
            'field' => 'jnature',
            'label' => 'Job Type',
            'rules' => 'required'
        ),
        array(
            'field'=>'assigne',
            'label'=>'Assigned From',
            'rules'=>'required'
        ),
        array(
            'field' => 'assignto',
            'label' => 'Assign To',
            'rules' => 'required'
        ),
        array(
            'field'=>'ddate',
            'label'=>'Due Date',
            'rules'=>'required'
        ),
        array(
            'field' => 'reminder',
            'label' => 'Reminder',
            'rules' => 'required|trim|max_length[1]|xss_clean'
        ),
//        array(
//            'field'=>'image',
//            'label' => 'Image',
//            'rules' => 'required'
//        )
//       

    )
);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion

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.