2016-10-04 71 views
0

我正在試圖將數據從窗體傳遞給控制器​​在codeigniter中...但是當我想打印一個表與結果...我在表的每一行上得到空。這裏是我的代碼:Codeigniter窗體序列化不起作用

FORM

<form class="col s12" id="update_form" name="update_form" method="post" > 
     <div class="row"> 
     <div class="input-field col s6"> 
      <input id="update_name" type="text" name="name" class="validate"> 
      <label for="first_name">Nombre</label> 
     </div> 
     <div class="input-field col s6"> 
      <input id="update_last_name" name="lastname" type="text" class="validate"> 
      <label for="last_name">Apellido</label> 
     </div> 
     </div> 
     <div class="row"> 
     <div class="input-field col s6"> 
      <input id="update_side" type="text" name="side" class="validate"> 
      <label for="partido">Partido</label> 
     </div> 
     <div class="input-field col s6"> 
      <input id="update_charge" type="text" name="charge" class="validate"> 
      <label for="cargo">Cargo</label> 
     </div> 
     </div> 
     <div class="row"> 
      <div class="input-field col s6"> 
       <div class="file-field input-field no-margin-top"> 
        <div class="btn light-blue darken-4"> 
        <span>Animación</span> 
        <input type="file"> 
        </div> 
        <div class="file-path-wrapper"> 
        <input class="file-path validate" name="animation" type="text"> 
        </div> 
       </div> 
      </div> 

      <div class="input-field col s6"> 
      <select id="update_section" name="section" autocomplete="off"> 
       <option value="" disabled selected>Seleccione una opción</option> 
       <option value="1">Presidencia</option> 
       <option value="2">Senadores</option> 
       <option value="3">Diputados</option> 
      </select> 
      <label>Sección</label> 
      </div> 
     </div> 
     <input type="hidden" name="update_politic_hide" id="update_politic_hdn" value=""> 
    </form> 

jQuery的

$("#update_politic_btn").click(function(event) { 
    /* Act on the event */ 

    var chango = $("#update_form").serialize(); 
    alert(chango); 
    $.post(baseurl + 'admin/update_politic', { 
      data: chango 
     }, 
     function(data) { 
      console.log(data); 
      list_politic(); 
     }); 
    event.preventDefault(); 
}); 

控制器

public function update_politic(){ 

    if ($this->input->is_ajax_request()) { 


    $params["name"] = $this->input->post("name"); 
    $params["lastname"] = $this->input->post("lastname"); 
    $params["side"] = $this->input->post("side"); 
    $params["charge"] = $this->input->post("charge"); 
    $params["animation"] = $this->input->post("animation"); 
    $params["section"] = $this->input->post("section"); 
    $params["id"] = $this->input->post("update_politic_hide"); 

    if ($params["section"]=="Presidencia") { 
     $params["section"]=1; 
    } 

    if ($params["section"]=="Senadores") { 
     $params["section"]=2; 
    } 

    if ($params["section"]=="Diputados") { 
     $params["section"]=3; 
    } 

    $this->load->model("politic"); 
    $this->politic->update($params); 
} 
} 

模型

public function update($param){ 

    $id = $param["id"]; 
    $values = array(

        "POLITIC_NAME" => $param["name"], 
        "POLITIC_LASTNAME" => $param["lastname"], 
        "POLITIC_SIDE" => $param["side"], 
        "POLITIC_CHARGE" => $param["charge"], 
        //"animation" => $param["animation"], 
        "SECTION_ID" => $param["section"], 
        ); 


    $this->db->update("politics",$values); 
    $this->db->where("POLITIC_ID",$id); 
} 

幫助!!!我不明白爲什麼一旦我想要打印結果我會得到空值!

+0

什麼是你返回你的Ajax調用? – madalinivascu

+0

你是否在'alert(chango);'中獲得了值? – Akshay

+0

@BarneyStinson是啊...所有的價值都在這一點上是正確的... –

回答

0

你的WHERE條件應該先像,

public function update($param){ 
    $id = $param["id"]; 
    $values = array(
     "POLITIC_NAME" => $param["name"], 
     "POLITIC_LASTNAME" => $param["lastname"], 
     "POLITIC_SIDE" => $param["side"], 
     "POLITIC_CHARGE" => $param["charge"], 
     //"animation" => $param["animation"], 
     "SECTION_ID" => $param["section"], 
    ); 
    $this->db->where("POLITIC_ID",$id); 
    $this->db->update("politics",$values); 
} 

向我們展示你的list_politic();代碼,如果它不能正常工作。

我認爲你是不正確的方式傳遞數據,如果你正在使用serialize()然後直接在$.post喜歡使用它,

$.post(baseurl + 'admin/update_politic',chango,function(data) { 
    console.log(data); 
    list_politic(); 
}); 
+0

list_politic是一個函數,它從數據庫加載表中的內容...它完美無缺! ...我嘗試你的答案,仍然得到相同的空錯誤 –

+0

我認爲數據沒有正確傳遞,請嘗試我更新的答案。 –

+0

YEAAH !!!有用!! ...這是問題!謝謝你,你應該得到一個格萊美!哈哈 –