dannymitza
dannymitza

Reputation: 59

AJAX not passing data to CodeIgniter Controller

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

Controller:

public function updateSocial($key){
        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();
        }
    }

Ajax:

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

                });
            }

and HTML:

                <div class="row">
                    <div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
                        <label for="{name}">{name}</label>
                        <input id="{name}" class="form-control" name="{name}"value="{address}"/>
                    </div>
                    <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12">
                        <div class="clearfix">&nbsp;</div>
                        <input type="submit" class="btn btn-material-green pull-right" onclick="updateSocial('{name}')"/>
                    </div>
                </div>

I've searched for solutions here on StackOverflow, tried everything and couldn't solve it.

If I manually enter values in url as following:

www.domain.com/controller/updatesocial/something&&another_something

it works, so the problem is not PHP-side.

What I'm trying to do: I have multiple input fields that needs to be modified individually, and I want to use AJAX to do so.

I'm sorry if same problem has been posted, but I couldn't find it. I've tried everything I could find and it doesn't work.

Upvotes: 0

Views: 5179

Answers (5)

Dixit Sourav
Dixit Sourav

Reputation: 390

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

Upvotes: 2

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

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.
                        }

                });

Upvotes: 2

simon
simon

Reputation: 11

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

Upvotes: 1

Rahul Bajaj
Rahul Bajaj

Reputation: 375

Replace this line

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

TO-

var key = name +"-"+ address;

Also explode on server with (-), Try it

Upvotes: 1

Disha V.
Disha V.

Reputation: 1864

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

Upvotes: 1

Related Questions