dqrfdl5708 2016-10-04 04:12
浏览 50
已采纳

Codeigniter表单序列化不起作用

i'm triying to pass data from form to controller in codeigniter...but when I want to print a table with the results...i'm getting null on every row of the table. Here is my code:

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();
});

Controller

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);
}
}

MODEL

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);
}

HELP!!! I don't understan why i'm getting null values once I want print results!!

  • 写回答

1条回答 默认 最新

  • doutan3463 2016-10-04 04:29
    关注

    Your where condition should come first like,

    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);
    }  
    

    And show us your list_politic(); code if it is not working.

    I think you are passing data in incorrect way if you are using serialize() then use it directly in $.post like,

    $.post(baseurl + 'admin/update_politic',chango,function(data) {
       console.log(data);
       list_politic();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计