2010-09-30 88 views
0

我有一个表单隐藏部分,直到提交正确的数据。我想通过jQuery和ajax来做到这一点。我希望我能演出的下一个元素表上,如果最后一块进入数据库确定,目前我的控制器看起来像这样,codeigniter和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来检查,最后数据提交给数据库确定,然后将下一个表单中的显示无更改为显示块?这全部可能吗?如何在显示表单的下一步之前检查数据是否已成功保存。

回答

0

简答:是的,这一切都有可能。怎么样 ?

首先JQuery的部分

您可以使用jQuery就像任何其他(非框架)站点。您的$.post()函数中的网址将如下所示。

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

验证部分

我建议你有几种选择。其一是您可以将数据提交给控制器功能,然后与模型或数据库一起使用。然后,您可以检索最后插入的行,并查看它是否与您在同一控制器功能中提交的数据相匹配。看看这个问题:Select last insert id

0

如果您想稍后用jQuery更改它,则不应该在add_career()中返回视图。

您应该返回保存()函数的结果,如果它返回

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

一个非常简单的解决方案可 首先将你的部分在二与 当前显示的部分使用隐藏输入字段将其值设置为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; 
     } 
});