Posts: 5
Threads: 1
Joined: Feb 2018
Reputation:
0
hi,
how to pass an array values to controller function using ajax call
this is my code in the view :
var selected=[];
var i = 0;
$('#multiselect_to option').each(function(){
selected[i]=$(this).val();i++;
});
console.log(selected);
$.ajax({
type: 'POST',
url : 'Book/essailivre',
data: selected,
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (result) {
alert('Success ');
window.location.href="http://............./Book/essailivre";
},
error: function (result) {
alert('Fail ');
}
});
Posts: 5
Threads: 1
Joined: Feb 2018
Reputation:
0
thanks for your answer
but
selected in my jqueru code is an ARRAY wher is the id of all selected options
eg. ["14", "2", "6", "8"] displayed on the chrome console F12
in my controller how can i access to the values of this array ?
Posts: 5
Threads: 1
Joined: Feb 2018
Reputation:
0
thx for your help
but still don't work in my controller can't display the values
in the view all work perfectly alert and consolelog
the problem in my controller always null values
my controller code :
public function essailivre()
{
echo json_decode($this->input->post(‘selected’));
}
MY VIEW CODE :
function displayVals() {
var selected=[];
var i = 0;
$('#multiselect_to option').each(function(){
selected[i]=$(this).val();i++;
});
selectedtext = JSON.stringify(selected);
console.log(selectedtext);
$.ajax({
type: 'POST',
url : 'Book/essailivre',
data: { selected : selectedtext },
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (result) {
alert(JSON.stringify(selected));
alert('Success ');
window.location.href="………...../index.php/Book/essailivre";
},
error: function (result) {
alert('Fail ');
}
});
}
Posts: 5
Threads: 1
Joined: Feb 2018
Reputation:
0
thanks for your help
still don't work
i think the problem is in the format of what i send to the controller
exactly the position of " or ' in the DATA : {name:val},
realy i don't know what i can do
Posts: 5
Threads: 1
Joined: Feb 2018
Reputation:
0
script code
function makeAjaxCall(){
$.ajax({
type: 'post',
url: 'Book/verifyUser',
cache: false,
data: $('#userForm').serialize(),
success: function(json){
try{
var obj = jQuery.parseJSON(json);
alert( obj['STATUS']);
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
}
CONTROLLER CODE
public function verifyUser() {
$userName = $this->input->$_POST['userName'];
$userPassword = $this->input->$_POST['userPassword'];
/*$userName = 'admin';
$userPassword = 'admin';*/
$status = array("STATUS"=>"false");
if($userName=='admin' && $userPassword=='admin'){
$status = array("STATUS"=>"true");
}
echo json_encode ($status) ;
}