Welcome Guest, Not a member yet? Register   Sign In
AJAX CALL
#1

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

https://stackoverflow.com/questions/8890...st-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"); 
Reply
#3

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 ?
Reply
#4

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

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


}
Reply
#6

Try this:

PHP Code:
url "<?php echo base_url('book/essailivre'); ?>"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

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
Reply
#8

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

https://developer.mozilla.org/en-US/docs...rk_Monitor
Reply
#9

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

}
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB