How to pass hidden field value via ajax to codeigniter controller How to pass hidden field value via ajax to codeigniter controller codeigniter codeigniter

How to pass hidden field value via ajax to codeigniter controller


Minor changes to javascript

$(function () {    var postData = {        "userid": $("input.userid").val(),        "vacancyid": $("input.vacancyid").val()    };    btnSave = $('#savebutton'),      ajaxOptions = {          type: 'POST',          url: "<?php echo base_url('dashboard/vacancy/saveVacancy);?>",          dataType: 'json'      };    btnSave.click(function (ev) {        var options = $.extend({}, ajaxOptions, {            //data : $(this).closest('form').serialize()            data: postData        });        ev.preventDefault();        // ajax done & fail        $.ajax(options).done(function (data) {            console.log(data);  // debug as an object            if (data.result === 'success') {                alert("Yeah, it saved userid " + data.userid + " to vacancy id " + data.vacancyid);            }        }).fail(function (xhr, status, error) {            console.warn(xhr);            console.warn(status);            console.warn(error);        });    });});

In the controller

public function saveVacancy(){    //assigning a more useable object name to the model during load    $this->load->model('user/usersavedvacancies_model', 'save_vacancy');     $data = array(        'userid' => $this->input->post('userid'),        'vacancyid' => $this->input->post('vacancyid')    );    //send data to model and model returns true or false for success or failure    $saved = $this->save_vacancy->doSaveId($data); //yes, I made up the method, change it    $result = $saved ? "success" : "failed";    echo json_encode(array('result' => $result, 'userid' => $data['userid'], 'vacancyid' => $data['vacancyid']));}


You need to understand that $.ajax takes two methods i.e GET and POST and from the documentation you can see that default method is GET so Since you have not defined method as GET/POST probably the method is taken GET so first change define ajax method to POST as well as you need to be clear about dataType of ajax it may be one of JSON/html and default is json.

$.ajax({  method: "POST",  url: url,  data: data,  dataType:'html'});

I guess this helped you can learn detail from Learn more.