CodeIgniter Forums
AJAX CALL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: AJAX CALL (/showthread.php?tid=70061)

Pages: 1 2


AJAX CALL - ignitercode2017 - 02-14-2018

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


RE: AJAX CALL - Kaosweaver - 02-14-2018

https://stackoverflow.com/questions/8890524/pass-array-to-ajax-request-in-ajax

so change you data line to:
PHP Code:
data: {selected:selected}, 

and in the controller, you access the variables by:
PHP Code:
$selected $this->input->post("selected"); 



RE: AJAX CALL - ignitercode2017 - 02-15-2018

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 ?


RE: AJAX CALL - InsiteFX - 02-15-2018

Convert to a JSON String :

This method you need to convert the JavaScript array to JSON String.
And than we will pass it to Ajax than we will receive it on php than decode the JSON there.
So it’s extra task to compile the JSON  string and decode it again.
But it’s a classic method to work on arrays from JavaScript to php.

Code:
var yourJSONText = JSON.stringify( arrayfromjs );
$.ajax({
      type: "POST",
      url: "to_codeigniter_controller_method",
      data: { phpArray : yourJSONText },
      success: function() {
             alert("Success");
       }
});

And your php will be like this.

PHP Code:
$newPhpArray json_decode($this->input->post('phpArray')); 

That’s it.


RE: AJAX CALL - ignitercode2017 - 02-15-2018

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


}


RE: AJAX CALL - InsiteFX - 02-15-2018

Try this:

PHP Code:
url "<?php echo base_url('book/essailivre'); ?>"



RE: AJAX CALL - ignitercode2017 - 02-18-2018

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


RE: AJAX CALL - Paradinight - 02-18-2018

show the javascript code and the controller code. Plz Use the code tag.

https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor


RE: AJAX CALL - ignitercode2017 - 02-18-2018

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

}


RE: AJAX CALL - Paradinight - 02-18-2018

(02-18-2018, 03:32 AM)ignitercode2017 Wrote: 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) ;

}

$this->input->$_POST['userName']; change to $this->input->post('userName');

You do not need jQuery.parseJSON, if you set the header to json.
e.g.
Code:
$this->output->enable_profiler(FALSE)->set_content_type('application/json')->set_output(json_encode($json));

pls use the code tag.