Welcome Guest, Not a member yet? Register   Sign In
passing data from AJAX to Controller
#1

[eluser]hrg.vincent[/eluser]
I tried to passing data from ajax to controller but only first 2 data will to send, the 3rd and 4th is empty. If I moved the 3rd and 4th become 1st and 2nd, then the previous 1st and 2nd become 3rd and 4th then also is not passing to controller for 3rd and 4th. Anyone can help on this?

AJAX
Code:
function loadVisaStatusFn()
{
var cs_vx_no = $('#cs_vx_no').val();
var cs_passport = $('#cs_passport').val();    
var tcs_fname = $('#tcs_fname').val();  
var tcs_passport = $('#tcs_passport').val();

$.ajax({
  type: "GET",
  url: '<?php echo site_url('ajax/getVisaStatus'); ?>/'+cs_vx_no+'/'+cs_passport+'/'+tcs_fname+'/'+tcs_passport,

  success: function(r){
   handleErrorFn(r);
   $("#rp-holder").html(r);
  }
});
}

Controller
Code:
public function getVisaStatus($cs_vx_no, $cs_passport, $tcs_fname, $tcs_passport)
{
$this->load->helper('url');
$this->load->helper('form');
  
$cs_vx_no = strtoupper($cs_vx_no);
$cs_passport = strtoupper($cs_passport);
$tcs_fname = strtoupper($tcs_fname);
$tcs_passport = strtoupper($tcs_passport);
  
$this->load->model('Data_m');
        $data['visa_status'] = $this->Data_m->getVisaStatus($cs_vx_no, $cs_passport, $tcs_fname, $tcs_passport);
$this->load->view('ajax/visa_status', $data);
}
#2

[eluser]Otemu[/eluser]
Hi,

Test using url segments and see if that works, example:
Code:
$cs_vx_no = strtoupper($this->uri->segment(n));
$cs_passport = strtoupper($this->uri->segment(n));
$tcs_fname = strtoupper($this->uri->segment(n));
$tcs_passport = strtoupper($this->uri->segment(n));
n being the correct url segment number
#3

[eluser]ivantcholakov[/eluser]
Why these parameters should be present within the URL? You may use POST method instead.

Code:
function loadVisaStatusFn()
{
var cs_vx_no = $('#cs_vx_no').val();
var cs_passport = $('#cs_passport').val();    
var tcs_fname = $('#tcs_fname').val();  
var tcs_passport = $('#tcs_passport').val();

$.ajax({
  type: "POST",
  url: '<?php echo site_url('ajax/getVisaStatus'); ?>',
  data: {
    cs_vx_no : cs_vx_no,
    cs_passport : cs_passport,
    tcs_fname : tcs_fname,
    tcs_passport : tcs_passport
  },
  success: function(r){
   handleErrorFn(r);
   $("#rp-holder").html(r);
  }
});
}
#4

[eluser]Unknown[/eluser]
You should send your ajax request with POST as suggested by ivantcholakov.

If you consider using POST this method, you need to protect your controller with :

Code:
if ($this->input->is_ajax_request())
{
   // code
}
else
{
    die("No direct access allowed, go eat some bacon");
}

Secondly, if you are using the csrf protection of CodeIgniter, you need to add your CSRF Token to your post data.
In config/config.php you have section about it.

Just send your token name and your key with your post data.

Code:
function loadVisaStatusFn()
{
var cs_vx_no = $('#cs_vx_no').val();
var cs_passport = $('#cs_passport').val();    
var tcs_fname = $('#tcs_fname').val();  
var tcs_passport = $('#tcs_passport').val();

$.ajax({
  type: "POST",
  url: '<?php echo site_url('ajax/getVisaStatus'); ?>',
  data: {
    cs_vx_no : cs_vx_no,
    cs_passport : cs_passport,
    tcs_fname : tcs_fname,
    tcs_passport : tcs_passport,
    csrf_token_name: "<?= $this->security->get_csrf_hash(); ?>" /* in the future of codeigniter it'll be unecessary with uri exclusions */
  },
  success: function(r){
   handleErrorFn(r);
   $("#rp-holder").html(r);
  }
});
}
#5

[eluser]hrg.vincent[/eluser]
[quote author="ivantcholakov" date="1383918803"]Why these parameters should be present within the URL? You may use POST method instead.

Code:
function loadVisaStatusFn()
{
var cs_vx_no = $('#cs_vx_no').val();
var cs_passport = $('#cs_passport').val();    
var tcs_fname = $('#tcs_fname').val();  
var tcs_passport = $('#tcs_passport').val();

$.ajax({
  type: "POST",
  url: '<?php echo site_url('ajax/getVisaStatus'); ?>',
  data: {
    cs_vx_no : cs_vx_no,
    cs_passport : cs_passport,
    tcs_fname : tcs_fname,
    tcs_passport : tcs_passport
  },
  success: function(r){
   handleErrorFn(r);
   $("#rp-holder").html(r);
  }
});
}
[/quote]

I've follow your code, that is using POST method, but I can't get the data from Controller, anything wrong from my code in controller?
#6

[eluser]ivantcholakov[/eluser]
The controller should be changed accordingly, into something like this:

Code:
public function getVisaStatus()
{
$this->load->helper('url');
$this->load->helper('form');
  
$cs_vx_no = strtoupper($this->input->post('cs_vx_no'));
$cs_passport = strtoupper($this->input->post('cs_passport'));
$tcs_fname = strtoupper($this->input->post('tcs_fname'));
$tcs_passport = strtoupper($this->input->post('tcs_passport'));
  
$this->load->model('Data_m');
        $data['visa_status'] = $this->Data_m->getVisaStatus($cs_vx_no, $cs_passport, $tcs_fname, $tcs_passport);
$this->load->view('ajax/visa_status', $data);
}

Make the controller working first, then you may pay attention on MarceauKa's advice about protection. Also, if you have not thought about it yet, check the model's method $this->Data_m->getVisaStatus(...) whether it is protected against SQL injections.




Theme © iAndrew 2016 - Forum software by © MyBB