2012-12-05 95 views
1

嗨,大家好我的表單似乎在提交和jquery中正常工作,雖然有兩件事:它似乎沒有采取信息通過,因爲它不斷添加0,0,0,0,而不是我在表單中提交的東西。接下來它不會刷新頁面?可能jquery失蹤了?在模型codeigniter jquery表單提交

插入功能:

function entry_insert(){ 
    $this->load->helper('form'); 
    $this->load->helper('html'); 
    $this->load->database(); 
    $data = array(
       'title'=>$this->input->post('title'), 
       'url'=>$this->input->post('url'), 
      'jscore'=>$this->input->post('jscore'), 
      'kscore'=>$this->input->post('kscore') 
      ); 
    $this->db->insert('movies',$data); 
    } 

這是得到由jquery的稱爲加載模型的控制器功能:

<form class="form-signin" method="post" id="form-signin"> 
    <h4 class="form-signin-heading">Add a Movie</h4> 
    <input type="text" class="input-block-level" placeholder="Movie Title" id="title"> 
    <input type="text" class="input-block-level" placeholder="URL" id="url"> 
    <input type="number" class="" min="1" max="100" placeholder="Jamies Score" id="jscore"> 
    <input type="number" class="" min="1" max="100" placeholder="Kellys Score" id="kscore"> 
    <button class="btn btn-large btn-primary" type="submit">Add</button> 
    </form> 
:視圖內

public function addmovie() 
{ 
     $this->load->model('Movie_model', '', TRUE); 
     $this->Movie_model->entry_insert(); 
     return true; 


} 

HTML形式

最後是jquery:

<script> 
$(function(){ 
    $("#form-signin").submit(function(){ 
    dataString = $("#form-signin").serialize(); 

    $.ajax({ 
     type: "POST", 
     url: "http://xxx.com/xx/index.php/welcome/addmovie", 
     data: dataString, 

     success: function(data){ 
      alert('Successful!'); 
     } 

    }); 

    return false; //stop the actual form post !important! 

    }); 
}); 
</script> 

任何人都可以發現我做錯了什麼?也許幫助我的jQuery刷新?

回答

0

您的任何輸入都沒有name屬性,這是讀取服務器端的值所必需的,您的serialize()調用可能也不會返回任何內容。我想你認爲這就是id的作用,但事實並非如此。

例子:

<input placeholder="Kellys Score" id="kscore"> 

應該是:

<input placeholder="Kellys Score" name="kscore" id="kscore"> 
<!--     Needs this ^^^^^^^^^^^^^   --> 
+0

非常感謝!將永遠不會猜到,它的作品馬上完美:)任何機會,你可以幫助刷新自動? – user1250526

+0

我不知道你想要「刷新」什麼。成功回調中的'data'參數將會得到服務器的響應,所以請按照您的意願進行操作。無論您的JavaScript需要做什麼,服務器都會返回。 –

+0

location.reload();它是否將它添加到jquery :)謝謝 – user1250526