2014-03-13 41 views
0

我目前正在嘗試使用JQuery/Ajax來調用PHP(CodeIgniter)函數並返回一個數組或一個使用JSON的布爾,我需要一點點(或者可能是很多)的幫助。JSON/AJAX和PHP與CodeIgniter之間的JSON

我有一個基本的電子郵件形式(名稱/電子郵件/消息)在一個jQuery對話框......

function emailForm() { 
    $('#emailDialog').dialog({ 
     autoOpen: false, 
     height: 450, 
     width: 300, 
     position: ['center-top'], 
     show: 'fade', 
     title: 'Contact Us', 
     modal: true, 
     buttons: { 
      Send: function() { 
       sendEmail(); 
      }, 
      Reset: function() { 
       clearForm('email'); 
      } 
     } 
    }); 
    $('#emailDialog').dialog('open'); 
} 

此使用Ajax提交到使用form_validation,要麼返回PHP/CI一個笨控制器在陣列(JSON)錯誤或發送郵件,並返回基於它的成功布爾...

class Email extends CI_Controller { 

function index() { 
    $this->load->library('form_validation'); 

    $this->form_validation->set_error_delimiters('', ''); 

    $this->form_validation->set_rules('name', 'Name', 'required'); 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('message', 'Message', 'required'); 

    if ($this->form_validation->run() == false) { 
     $data = array(
      'name' => form_error('name'), 
      'email' => form_error('email'), 
      'message' => form_error('message') 
     ); 
     header('Content-Type: application/x-json; charset=utf-8'); 
     echo json_encode($data); 
     return false; 
    } else { 
     $this->load->library('email'); 
     $this->email->set_newline("\r\n"); 

     $name = $this->input->post('name'); 
     $email = $this->input->post('email'); 
     $message = $this->input->post('message'); 

     $this->email->from($email, '...'); 
     $this->email->to('...'); 
     $this->email->subject('...'); 
     $this->email->message($message); 

     if ($this->email->send() == false) { 
      header('Content-Type: application/x-json; charset=utf-8'); 
      echo(json_encode(false));     
     } else { 
      header('Content-Type: application/x-json; charset=utf-8'); 
      echo(json_encode(true)); 
     } 
    } 
} 
} 

我想阿賈克斯解析返回和相應的行爲基於以下代碼然而,當我提交一個空白表格(這應該會導致錯誤),用戶代理總是ev對else子句(指示郵件已成功發送)而不是初始if子句(解析JSON數組並呈現HTML)產生影響。我嘗試過幾種不同的if/else if/else配置,無濟於事。此外,調試時,郵政實際上是顯示空白值爲每個參數,以及響應和JSON顯示什麼,我覺得是正確的數據...

POST:

email 
message 
name 

迴應:

{"name":"The Name field is required.","email":"The Email field is required.","message":"The Message field is required."} 

JSON:

name "The Name field is required." 
email "The Email field is required." 
message "The Message field is required." 

我似乎無法確定爲什麼我的阿賈克斯回報總是評估爲真,所以任何幫助將不勝感激。先謝謝你!

請不要downvote我太嚴厲,我是很新,SO

回答

2

語句如果你返回false告訴客戶端:喂他們是錯誤的,我建議你到響應狀態標題來更改到500然後處理jquery/ajax調用的錯誤部分中的錯誤。

這裏的爲例,我希望它會幫助你

服務器端:

// Set your rules 

$this->form_validation->set_rules('name', 'Name', 'required'); 
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 
$this->form_validation->set_rules('message', 'Message', 'required'); 

if ($this->form_validation->run()) { 
    //happy happy time 
} 
else { 
    //well now i'm sad... 

    //Important to turn that off if it's on 
    $this->output->enable_profiler(false); 

    //Since you're using CI why not use CI ?? 
    $this->output->set_status_header('500'); 
    $this->output->set_content_type('application/json'); 

    echo json_encode(array(
     'error_msg' => validation_errors(), 
)); 
} 

在客戶端:

$.ajax({ 
    type: 'POST', 
    url:  "<?php echo site_url('your url');?>", 
    data: {the post data}, 
    dataType:'JSON', 
    success: function(data) { 
     //happy success   

    }, 
    error: function(data) { 
     //sad error 
     $("#error-message-selector").html('').append(data.responseJSON.error_msg); 
    } 
    }); 
+0

@jbeckom高興能幫上忙! – Gabtheviking

+0

FANTASTICO !!!!!並感謝讓我接觸CI的方式,這樣更容易! – jbeckom

0

你爲什麼不使用你從控制器後的反應如何?

,如果有一個錯誤在你的控制器 結束時顯示錯誤 其他 顯示成功消息

嘗試總是echo json_encode($data);你可以在控制器中設置一個標誌$data陣列像

$data = array(
// keys 
"success" => $success 
) 

$success = false在開始,然後如果驗證成功$success = true;

我還認爲你不需要標題()在所有