AJAX not passing data to CodeIgniter Controller

前端 未结 5 911
长情又很酷
长情又很酷 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:52

    Try this..

    You can send data to server by data option in ajax and the type which defines. The default type is GET method

    Use " data: {name:name,address:address},"

     $.ajax({
                        type: "GET",
                        url: "<?php echo base_url(); ?>controller/updatesocial", 
                        data: {name:name,address:address},
                        dataType: "text",  
                        cache:false,
                        success: 
                            function(data){
                                alert(data);  //as a debugging message.
                            }
    
                    });
    
    0 讨论(0)
  • 2021-01-16 18:52

    It might creating problem with "&&", Try data: encodeURIComponent(key) in ajax.

    0 讨论(0)
  • 2021-01-16 18:56
    function updateSocial(name){
                var address = $("#" + name).val();
                var key = name +"&&"+ address;
                //alert(key);
                $.ajax({
                    type: "GET",
                    url: "<?php echo base_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();
        }
    }
    
    0 讨论(0)
  • 2021-01-16 19:00

    As far as I know ,the problem is at you "var key",change it to : var key = "name="+name+"&"+"address="+address;

    0 讨论(0)
  • 2021-01-16 19:06

    Replace this line

    var key = name +"&&"+ address;
    

    TO-

    var key = name +"-"+ address;
    

    Also explode on server with (-), Try it

    0 讨论(0)
提交回复
热议问题