Codeigniter:Ajax数据在日志中打印但在函数中不起作用

时间:2016-05-31 11:02:55

标签: php ajax codeigniter

我通过ajax获取数据的功能是:

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

    $("#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",

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

        success: function(res)
        {
          console.log(res);
          // window.alert("i got some data ");
          if (res)
          {
              jQuery("div#result").show();
          }
        },
        fail: function(res)
        {
          console.log(res);
        }
      });
    });
  });

控制器功能我有:

public function update_season()
{
    $session_id = $this->session->userdata('id');
    if (isset($session_id)) 
    {

        // print_r($_POST);
        // die();
        $update_id = $this->input->post('hiddenValue');
        $update_name = $this->input->post('update_name');

        $arr = array(
        'id' => $update_id,
        'name'=> $update_name);

        //This prints empty data
        // print_r($arr);
        // die();

        $result = $this->model_season->edit_season($arr);
        // $result = $result->row();

        if ($result) 
        {
            print_r($arr);
        }

        else
        {
            return FALSE;
        }

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

在模型中通过控制器我有:

public function edit_season($data)
{
      // I am getting right array of name and id
      print_r($data);
      die();

    // but get empty variable if i try to assign value to it
    $name = $data['name'];

    $this->db->where('seasons', array('season_id ' => $data['id']));
    $query = $this->db->update('seasons',array('names ' => $data['name'] ));

    if ($query) 
    {
        return $query;
    }
    else
    {
        return FALSE;
    }

}

ajax似乎工作正常,因为它打印idname的值,我甚至没有在json中编码它,但我无法得到它单独变量中的值。我想知道是否有任何不同的方法从ajax数据中获取值?
当我让它运行整个模型功能而不使它死亡时我有以下错误:
UPDATE {季节{1}} {名称{1}} {季节{1}} Array`` 像数组中没有任何东西

2 个答案:

答案 0 :(得分:1)

您的查询中有错误,您要将数组提供给where条件,其中应该是string,

$this->db->where('season_id ', $data['id']);

此外,在'season_id '应该'season_id'

等条件下,有不必要的空格(尽管CI驱动程序内部修剪所有空格)并不好
$this->db->where('season_id', $data['id']);
$query = $this->db->update('seasons', array('names' => $data['name']));

在此处检查驾驶员身份验证:Queries in CI

答案 1 :(得分:1)

    /**
     * Método que crea un nuevo usuario en el sistema
     * y lo guarda en el arrayList de usuarios
     */
    public static void crearUsuario()
    {      
        JPanel panelCrearUsuario = new JPanel();
        JDialog frame = new JDialog();     

        //label nuevo usuario
        panelCrearUsuario.add(new JLabel("Nuevo Usuario: "));

        //textbox nuevo usuario
        JTextField jtNombreUsuario = new JTextField(15);
        panelCrearUsuario.add(jtNombreUsuario);

        //label DNI
        panelCrearUsuario.add(new JLabel("DNI usuario: "));

        //textbox DNI
        JTextField jtDNIUsuario = new JTextField(15);
        panelCrearUsuario.add(jtDNIUsuario);

        //botón crear usuario
        JButton JButtonCrearUsuario = new JButton("Crear usuario");

 JButtonCrearUsuario.addActionListener(new ActionListener()   {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Debbuging the IDE doesn't enter here :(
                System.out.println("blablabla");
                JButtonCrearUsuarioEvento(u);
            }

        });

        panelCrearUsuario.add(JButtonCrearUsuario);

        frame.getContentPane().add(panelCrearUsuario); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.pack();
        frame.setSize(420, 460);
        frame.setModal(true);
        frame.setVisible(true); 

        Usuario u = new Usuario();
        u.setNombre(jtNombreUsuario.getText());
        u.setDNI(jtDNIUsuario.getText());



    }