views:

49

answers:

2

I have a form that has sections hidden until, the correct piece of data is submitted. I am wanting to do this through jQuery and ajax. I was hoping that I would be able show the next element on the form if the last piece entered into the database ok, currently my controller looks like this,

function add_career() {
    $data = array();
    $this->load->model('admin_model');
    $this->load->library('form_validation');

        if($this->input->post('career_set') == 'Save') {

            $this->form_validation->set_rules('career_name', 'Career name', 'required|min_length[3]');
            $this->form_validation->set_rules('career_desc', 'Career description', 'required|max_length[3000]');
            $this->form_validation->set_rules('useful_info', 'Useful Information', 'max_length[1000]');
            $this->form_validation->set_rules('useful_links', 'Useful Links', 'max_length[1000]');
            if ($this->form_validation->run() == FALSE) {
               $this->template->build('admin/add_career'); 
            } else {
               if($this->input->post('degree_needed')) {
                   $degree_needed = 'Yes';
               } else {
                   $degree_needed = 'No';
               }

               $this->load->model('careers');
               $insertCareer = $this->careers->save(
                 $this->input->post('career_name'),
                 $this->input->post('career_desc'),
                 $degree_needed,
                 $this->input->post('useful_info'),
                 $this->input->post('useful_links')
               );

               $insertCareer['career_id'] = $this->db->insert_id();

               //save the data in the session, so we can to it if need be
                $this->session->set_userdata(array('career' => $insertCareer));
                $this->firephp->log($this->session->userdata);    
               }

        }
        $careerData = $this->session->userdata('career');
        if($this->input->post('salary_set') == 'Save') {
                $this->form_validation->set_rules('basic_salary', 'Basic salary', 'required|max_length[12]');
                $this->form_validation->set_rules('trained_salary', 'Fully trained salary', 'required|max_length[12]');
                $this->form_validation->set_rules('progressed_salary', 'Progressing onto salary', 'required|max_length[12]');
                $this->form_validation->set_rules('average_salary', 'Average salary', 'required|max_length[12]');

                if ($this->form_validation->run() == FALSE) {
                        $this->template->build('admin/add_career'); 
                } else {
                    $this->load->model('salaries');
                    $insertSalary = $this->salaries->save(
                        $this->input->post('basic_salary'),
                        $this->input->post('trained_salary'),
                        $this->input->post('progressed_salary'),
                        $this->input->post('average_salary'),
                        $careerData['career_id']
                    );

                $this->session->set_userdata(array('salary' => $insertSalary));
                $this->firephp->log($this->session->userdata);    
                }
        }

        if($this->input->post('course_grades_set') == 'Save') {
            //first off we need to save the grade details

            $this->load->model('grades');
            $this->load->model('course');
            $this->firephp->log(count($_POST['grade_desc']));

            foreach ($_POST['grade_desc'] as $k => $v) {
                $this->firephp->log($v, 'Looped Results');
                $insertGrade = $this->grades->save($v, $careerData['career_id']);
                // theorertically we should be able to save the assicated course at the same time using $k
                $insertCourse = $this->course->save(
                    $_POST['course_type'][$k],
                    $_POST['course_names'][$k], 
                    $_POST['course_links'][$k],
                    $this->db->insert_id()
                );
                $this->firephp->log($insertGrade, $k);
                $this->firephp->log($insertCourse, $k);
            }
            //$insertGrades = $this->grades->save()
            //);
        }




   $this->template->build('admin/add_career', $data);
}

I basically need to my ajax to check that the last data was submitted to the data base ok and then change the display none on the next form to display block? Is this all possible? How would I check that the data has succesfully been saved before showing the next step of the form.

A: 

Short answer: yes it is all possible. How ?

First the JQuery part

You can use jQuery just as you would any other (non-framework) site. Your url in a $.post() function would look like this.

$.post(
    'http://example.com/index.php/controller/function_name",
    {key1:value1,key2:value2},
    function(data){
        //function to execute when return data received from 'function_name' in url
        //this is where you would show the next step of the form
    },
    type of data being returned(html, json)
    );

The validation part

I suggest that you have a few options. One is that you can submit your data to a controller function that then works with a model or the database. You can then retrieve the last inserted row and see if it matches your submitted data within the same controller function. Check out this question: http://stackoverflow.com/questions/2266604/select-last-insert-id

kevtrout
A: 

You shouldn't return the view in the add_career() if you want to change it later with jQuery.

You should return the result of the save() function, if it returns

$this->db->affected_rows() >= 0;

then you can check the response text and do what you want from there...

NOTE: I would rather return a XML file and use this aproach:

$.ajax({
    url: "the/path/to/the/function/",
    type: 'POST',
    dataType: 'xml',
    data: $('#your_form').serialize(),
    timeout: 15000,
    error: function(){

    },
    success: function(xml){
        //and away we go....
    }
});
Mario Cesar