AJAX not passing data to CodeIgniter Controller

前端 未结 5 910
长情又很酷
长情又很酷 2021-01-16 18:27

Greeting StackOverflow! I am trying to pass data to controller using AJAX, but for some reasons it doesn\'t.

Controller:

public function updateSocial         


        
5条回答
  •  清酒与你
    2021-01-16 18:56

    function updateSocial(name){
                var address = $("#" + name).val();
                var key = name +"&&"+ address;
                //alert(key);
                $.ajax({
                    type: "GET",
                    url: "controller/updatesocial/"+key, 
                    dataType: "text",  
                    cache:false,
                    success: 
                        function(data){
                            alert(data);  //as a debugging message.
                        }
    
                });
            }
    

    You are sending data using GET method so you have to send it by appending url and your controller will be

    public function updateSocial(){
        $key = $this->uri_segment(3);
        if($key != null){
            list($name, $address) = explode("&&", $key);
            $this->db->query("UPDATE social SET address = '" . $address . "' WHERE name = '" . $name . "'");
            echo "Affected rows: " . $this->db->affected_rows();
        }
    }
    

提交回复
热议问题