2013-07-21 24 views
0

我使用Codeigniter作爲我的框架,並有一個簡單的聯繫表單。這使用表單助手,並且如果AJAX不存在,我使用AJAX和控制器中的回退。AJAX窗體將只顯示成功消息或發佈數據到數據庫不是兩個

目前,我的代碼只顯示來自ajax表單的成功消息,或將數據發佈到數據庫,具體取決於我是否在控制器中更改它們 - 我的錯誤消息工作正常。

我很困惑,它不會同時發佈和顯示成功消息 - 我想我可能會缺少我的控制器或AJAX請求中的東西?

這裏是我的代碼作爲指導,如果任何人都可以發現任何事情會很棒,因爲它現在正在讓我緊張!

*我現在發佈的代碼可以將數據發佈到數據庫中。當我移動下面這個職位的數據元素 - >return $this->output->set_output(json_encode($respond)); It doesn't post to the database but shows the success message and vice versa.

控制器

// if ajax request 
     if($this->input->is_ajax_request()) { 
      $respond = array(); 
      if($this->form_validation->run() == FALSE) { 
       $respond['result'] = 'false'; 

       $respond['error_message'] = $error_message; 
       $respond['errors'] = validation_errors(); 

       // set individual errors - for warning classes 
       $respond['first_name_error'] = form_error('first_name'); 
       $respond['country_error'] = form_error('country'); 
       $respond['email_error'] = form_error('email'); 
       $respond['message_error'] = form_error('message'); 

      } else { 

       $respond['result'] = 'true'; 
       $respond['success_message'] = $success_message; 

       // add contact message to the database 
       $this->contact_model->insert_contact_message($curr_lang, $this->input->post('first_name'), $this->input->post('country'), $this->input->post('email'), $this->input->post('phone'), $this->input->post('message')); 
      } 
      return $this->output->set_output(json_encode($respond)); 

     } else { 
      // if ajax request failed - use CI 
      if($this->form_validation->run() == FALSE) { 

       $data['error_message'] = $error_message; 
       $data['errors'] = validation_errors(); 
      } else { 
       // add contact message to the database 
       $this->contact_model->insert_contact_message($curr_lang, $this->input->post('first_name'), $this->input->post('country'), $this->input->post('email'), $this->input->post('phone'), $this->input->post('message')); 

       $data['success_message'] = $success_message; 

      } 
     } 

     // set field labels 
     $data['first_name'] = $first_name; 
     $data['country'] = $country; 
     $data['email'] = $email; 
     $data['phone'] = $phone; 
     $data['message'] = $message; 

     // initialize view name 
     $data['content'] = $page; 

     // load the view 
     $this->load->view('template', $data); 
    } 

AJAX

$('#submit').click(function(e) { 
     e.preventDefault(); 
     // send the form data to the controller 
     $.ajax({ 
      url: $(this).attr('action'), 
      type: 'POST', 
      data: $('form').serialize(), 
      dataType: 'json', 
      success: function(respond) { 
       if(respond.result === 'false'){ 
        // function to add warning class 
        function add_error(response, field){ 
         if(response){ 
          $(field).addClass('warning'); 
         } 
        } 

        // add warning classes - doing this individually as some inputs have more than one error message 
        add_error(respond.first_name_error, 'input[name="first_name"]'); 
        add_error(respond.country_error, 'input[name="country"]'); 
        add_error(respond.email_error, 'input[name="email"]'); 
        add_error(respond.message_error, 'textarea'); 

        // post all errors to the view 
        var error_msg = respond.error_message + respond.errors; 
        $('#error_message').html(error_msg);  

       } 
       if(respond.result === 'true'){ 
        // empty the form 
        $('#error_message').empty(); 
        $('form').find("input[type=text], textarea").val(''); 

        // set the success message 
        var success_msg = respond.success_message; 
        $('#success_message').html(success_msg).fadeOut(6000);     
       } 
      }  
     }); 
     return false; 
    }); 

回答

0

這可能是因爲你沒有解析JSON響應讓你如果是政治家ts永遠不會是真的(因爲response.result可能正在評估'undefined')。

+0

那麼我究竟會如何解析JSON else語句呢?對不起,如果這很簡單,但新的應用AJAX。 – user2212564

+0

你可以嘗試類似var response = JSON.parse(respond); –

+0

不幸的是,返回錯誤說SyntaxError:JSON.parse:螢火蟲嗯意外字符 – user2212564

0

在你的Ajax respond.result === true或false不是'true'或'false'。你只需要刪除引號,因爲它是一個布爾值而不是字符串。

相關問題