Codeigniter:Ajax发送id& name但是函数只接收id

时间:2016-05-31 06:06:38

标签: javascript php jquery ajax codeigniter

我得到了身份但不是名字,我在做什么? 请看下面我的CI代码和ajax函数:
这是查看,我正在调用ajax:

 <div class="modal fade" id="myModal<?php echo $values->season_id; ?>"
    tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
     <div class="modal-dialog" role="document">
         <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Update Seasons</h4>
             </div>
             <div class="modal-body">
               <div class="form-group">
                 <input type="hidden" id="hiddenValue" name="hiddenValue" value="<?php echo $values->season_id; ?>">
                     <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Season Name: 
                     </label>
                     <div class="title_right">
                         <div class="input-group">
                             <input type="text" class="form-control" name="update_name" id="update_name" value="<?php echo $values->names; ?>">
                          </div>
                      </div>
               </div>
             </div>
             <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <input type="submit" class="btn btn-primary" id="submit" value="Save changes">
             </div>
            </div>
          </div>
        </div>

这是Ajax功能:

<script type="text/javascript"> 
// Ajax post
  $(document).ready(function()
  {

    alert("Ajax function started working");

    $("#submit").click(function(event)
    {
      event.preventDefault();
      var hiddenValue = $("#hiddenValue").val();

      alert(hiddenValue);

      var update_name = $("input#update_name").val();

      // pop up Name Entered
      alert(update_name);

      jQuery.ajax(
      {
        type: "POST",
        url: "<?php echo base_url(); ?>" + "seasons/update_season",
        dataType: 'json',

        data: {
          hiddenValue : hiddenValue, 
          update_name: update_name
        },

        success: function(res)
        {
          console.log(res);
          // window.alert("i got some data ");
          if (res)
          {
            alert("changes made");
          }
        }
      });
    });
  });

在这里,我试图捕捉价值:

public function update_season()
{
    $session_id = $this->session->userdata('id');
    if (isset($session_id)) 
    {
        $update_id = $this->input->post('hiddenValue');
        $update_name = $this->input->post('update_name');

        $result = $this->model_season->update_season($update_id,$update_name);

        if ($query) 
        {
            $query = $query->row();
            $data  = array(
                'season_id' => $query->season_id,
                'names' =>$query->names 
                );
            echo json_encode($data);
        }
        else
        {
            return FALSE;
        }

    }
    else
    {
        redirect('user_authentication');
    }
}

1 个答案:

答案 0 :(得分:1)

您使用

 if ($query) 
    {
        $query = $query->row();
        $data  = array(
            'season_id' => $query->season_id,
            'names' =>$query->names 
            );
        echo json_encode($data);
    }

但是你的$query。作为你的代码它应该具有以下

if ($result) 
        {
            $data  = array(
                'season_id' => $result->season_id,
                'names' =>$result->names 
                );
            echo json_encode($data);
        }

现在使用$result->season_id代替$query->season_id,并确保在模型row()中使用update_season()方法返回所需的值