2016-02-22 159 views
0

我想在codeIgniter中使用ajax提交表單,但出現錯誤。在codeigniter中使用Ajax提交表單

這是我的視圖代碼;

<div class="form-group input-control"> 
    <p id="error1" style="display: none; color: green"><b>Registered Successfully.</b></p> 
    <p id="succ" style="display: none; color: red"><b>E-mail Alreay Registered.</b></p> 
    <button class="button" onclick="newsletter()"><span class="icon-envelop"></span></button> 
    <input type="text" class="form-control" placeholder="Your E-mail..." value="" id="newsletter" onblur="if (this.value == '') {this.value = 'Your E-mail...';}" onfocus="if(this.value == 'Your E-mail...') {this.value = '';}"> 
    <br> 
    <p id="error" style="display: none; color: red"><b>Please Enter E-mail.</b> 
     <p> 
      <p id="error2" style="display: none; color: red"><b>Error while request..</b> 
       <p> 
</div> 

這裏是我的腳本:

<script> 
    function newsletter() { 
     var email = $("#newsletter").val(); 
     if (email == "") { 
      $("#error").css("display", "block"); 
     } else { 
      $.ajax({ 
       type: "post", 
       url: "<?php echo site_url();?>/shopit/index/newsletter", 
       cache: false, 
       data: 'search=' + email, 
       success: function(response) { 
        if (response == "succ") { 
         $("#succ").css("display", "block"); 
         $("#error").css("display", "none"); 
         $("#error1").css("display", "none"); 
         $("#error2").css("display", "none"); 
        } else { 
         $("#succ").css("display", "none"); 
         $("#error").css("display", "none"); 
         $("#error1").css("display", "block"); 
         $("#error2").css("display", "none"); 
        } 
       }, 
       error: function() { 
        $("#succ").css("display", "none"); 
        $("#error").css("display", "none"); 
        $("#error1").css("display", "none"); 
        $("#error2").css("display", "block"); 
       } 
      }); 
     } 

    } 
</script> 

,這是我的控制器功能的代碼;

public function newsletter(){ 
    $search= $this->input->post('search'); 
    $result = $this->Index_model->newsletter($search); 
    echo $result; 
} 

,最後我的模型功能:

public function newsletter($search){ 
    $query = $this->db->get_where("shopit_newsletter", array('email'=>$search)); 
    $result = $query->result(); 

    if($query->num_rows() > 0){ 
      echo "error"; 
    }else{ 
     $data = array(
      "email"=>$search 
     ); 
     $this->db->insert("shopit_newsletter", $data); 
     echo "succ"; 
    } 
} 

這是拋出錯誤,並打算到錯誤,什麼是錯的這個代碼,請幫幫我,我用Google搜索,但沒有找到確切的結果。

這是控制檯快照。

console snapshot

+3

你做了什麼樣的錯誤?顯示控制檯輸出。 – StreetCoder

+0

傳遞給getElementById()的空字符串 –

+0

未清除您的評論。只需更多參考資料就可以正確解釋顯示控制檯或錯誤輸出的屏幕截圖 – StreetCoder

回答

0

所有型號的功能首先應該是

public function newsletter($search) 
{ 
    $query = $this->db->get_where("shopit_newsletter", array('email'=>$search)); 
    $result = $query->result();  
    if($query->num_rows() > 0) 
    { 
     return "error"; 
    } 
    $data = array(
     "email"=>$search 
    ); 
    $this->db->insert("shopit_newsletter", $data); 
    return "succ";   
} 

控制器應

public function newsletter(){ 
    $search= $this->input->post('search'); 
    $result = $this->Index_model->newsletter($search); 
    echo json_encode(array("data"=>$result)); 
} 

而且在Ajax請求

success: function(response) { 
if (response.data == "succ") {