I tried so many times to debug this code but it doesn't seem to work at all. I am using chosen jquery on my application.
Here is my code:
$(document).ready(function(){
var status = [];
var method = $(this).attr('data-method');
var config = {
'.chosen-select' : {},
'.chosen-select-deselect' : {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
'.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
'.chosen-select-width' : {width:"95%"}
}
for (var selector in config) {
$(selector).chosen(config[selector]);
}
$("#test").chosen().change(function(e, params){
console.clear()
$("#test :selected").each(function(i,selected)
{
status[i] = $.trim($(selected).text());
})
console.log(status);
var new_var = JSON.stringify(status);
$.ajax({
type: "POST",
url: "<?php echo site_url('request/result') ?>",
data: { data: new_var }
}).done(function(data) {
alert( "Data Send:");
}).fail(function() {
alert( "Data Not Sent" );
});
e.preventDefault();
}); });
The new_var holds the array values but i cant post its value to my controller.
Controller : request.php
public function result(){
echo json_encode($status);
die();
}
I am debugging it on firefox on the network tab i can see values on params but on the response tab it returns NULL I cant seem to solve the problem for almost a week. I do not have any ideas how to solve it because i'm new to AJAX
you need to read from $_POST array:
public function result(){
// Your variable is here:
var_dump($_POST['data']);
// echo json_encode($_POST['data']);
die();
}
Please remember to cleaning $_POST, eg using filter_input
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.