簡體   English   中英

Codeigniter和Ajax幫助

[英]codeigniter and ajax help

我有一個表格,其中隱藏了各個部分,直到提交正確的數據為止。 我想通過jQuery和Ajax做到這一點。 我希望如果最后一部分輸入到數據庫中就可以在表單上顯示下一個元素,目前我的控制器看起來像這樣,

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);
}

我基本上需要用我的Ajax來檢查最后一個數據是否已提交到數據庫ok,然后在下一個窗體上將顯示內容更改為display block? 這一切可能嗎? 在顯示表單的下一步之前,我將如何檢查數據是否已成功保存。

有一個非常簡單的解決方案首先將div划分為多個部分。 當前顯示的零件使用隱藏的輸入字段將其值設置為1。 將隱藏字段分配給所有div塊,並將其值設置為0。將類分配給所有輸入字段。 觸發ajax並成功響應后,執行此步驟隱藏當前div。

$('.class_name').click(function(){
         var value = $(this).val();
         if(value == 1){
              $(this).val() = 0;
              $(this).parent().hide();
         }else{
              $(this).parent().siblings('div').find('input').val() = 1;  
         }
});

簡短的回答:是的,一切皆有可能。 怎么樣 ?

首先是jQuery部分

您可以像使用其他任何(非框架)網站一樣使用jQuery。 $ .post()函數中的URL看起來像這樣。

$.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)
    );

驗證部分

我建議您有一些選擇。 一種是可以將數據提交到控制器功能,然后該控制器功能可以與模型或數據庫一起使用。 然后,您可以檢索最后插入的行,並查看其是否與您在同一控制器函數中提交的數據匹配。 看看這個問題: 選擇最后一個插入ID

如果以后要使用jQuery更改視圖,則不應在add_career()中返回視圖。

如果返回,則應返回save()函數的結果

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

然后您可以檢查響應文本並從那里做您想做的...

注意:我寧願返回XML文件並使用此方法:

$.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....
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM