2017-04-05 61 views
1

表單提交使用Jquery和Ajax不在服務器上工作,但它在localhost中完美工作。這是什麼原因。我使用Codeigniter框架。表單提交使用JQuery Ajax不在服務器上工作,但在localhost codeigniter中工作

View

//order.php 

<form action="" method="post" id="form_order"> 
    <p><input type="text" name="due_date" /></p> 
    <p><input type="text" name="name" /></p> 
    <p><button type="submit">SAVE</button></p> 
</form> 

<script> 
    $(document).ready(function(){ 

     (function($) { 
      "use strict"; 

       $(function() { 
        $('#form_order').validate({ 
         rules: { 
          due_date: { 
           required: true 
          }, 
          name: { 
           required: true 
          } 
         }, 
         messages: { 
          due_date: { 
           required: "date required" 
          }, 
          name: { 
           required: "name required" 
          } 
         }, 
         $('#loading').show(); 

         $(form).ajaxSubmit({ 
          type:"POST", 
          data: $(form).serialize(), 
          url:"<?php echo base_url("ordercode"); ?>", 
          success: function(data) { 
           $('#loading').hide(); 
           $('#success').show(); 
           $('#success').html(data); 
          }, 
          error: function() { 
           $('#loading').hide(); 
           $('#success').show(); 
           $('#success').html('Something went wrong. Tryagain later!'); 
          } 
         }); 
        } 
       }); 
      }); 

     })(jQuery) 
    }); 
</script> 

controller

//Ordercode.php 

if($this->input->post('new_order')) { 
     $due_date = $this->input->post('due_date'); 
     $name = $this->input->post('name'); 

     $add = $this->orders->addOrder($due_date, $name); 

     if($add === TRUE) { 
      echo 'Order Added.'; 
     } 
     else if($add === FALSE) { 
      echo 'Something went wrong. Tryagain later!'; 
     } 
     else { 
      echo 'Something went wrong. Tryagain later!'; 
     } 
} 

Model

//Orders.php 

public function addOrder($due_date_first, $name) { 
     $data = array(
      'order_due_date' => $due_date_first, 
      'order_name' => $name 
     ); 
     $add = $this->db->insert('tbl_order', $data); 

     if($add == 1) { 
      return TRUE; 
     } 
     else { 
      return FALSE; 
     } 
} 

上面的代碼完全在本地主機,但不是在服務器上。服務器總是返回錯誤。

Output

Server :出了錯。稍後再試!

​​訂單已添加。

我的編碼有問題嗎?爲什麼它不適用於服務器。有沒有解決辦法。謝謝。

+0

顯然有什麼不對這一行:$添加= $這個 - >訂單 - > addOrder($ DUE_DATE,$名);.它回來了什麼? – jeff

+0

@jeff:在'localhost'中它返回'TRUE'。但在'服務器'它顯示'出了點問題。 Tryagain後來!「來自Jquery。 –

+0

檢查「服務器」日誌文件的錯誤日誌並在此粘貼一些錯誤日誌。 –

回答

1
$(document).ready(function(){ 
    (function($) { 
     "use strict"; 
     $(function() { 
      $('#form_order').validate({ 
       rules: { 
        due_date: { 
         required: true 
        }, 
        name: { 
         required: true 
        } 
       }, 
       messages: { 
        due_date: { 
         required: "date required" 
        }, 
        name: { 
         required: "name required" 
        } 
       }, 
       submitHandler: function(form,e) { 
        e.preventDefault(); 
        var formData = new FormData(form); 
        $('#loading').show(); 
        $.ajax({ 
         type:"POST", 
         data: formData, 
         url:"<?php echo base_url("ordercode"); ?>", 
         success: function(data) { 
          $('#loading').hide(); 
          $('#success').show(); 
          $('#success').html(data); 
         }, 
         error: function() { 
          $('#loading').hide(); 
          $('#success').show(); 
          $('#success').html('Something went wrong. Tryagain later!'); 
         } 
        }); 
       } 
      }); 
     })(jQuery); 
    }); 
}); 

,並清除表單標籤action=""屬性

相關問題