2012-12-05 164 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