Greeting StackOverflow! I am trying to pass data to controller using AJAX, but for some reasons it doesn\'t.
Controller:
public function updateSocial
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.
}
});
It might creating problem with "&&", Try data: encodeURIComponent(key)
in ajax.
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();
}
}
As far as I know ,the problem is at you "var key",change it to : var key = "name="+name+"&"+"address="+address;
Replace this line
var key = name +"&&"+ address;
TO-
var key = name +"-"+ address;
Also explode on server with (-), Try it