简体   繁体   中英

how to pass ajax call parameters separately

I have a java script ajax method which send parameters to controller

function class_selection(day) {

        var section_id = document.getElementById('section_id2').value;
        var class_id = document.getElementById('class_id').value;
       // alert("day"+day);
        alert("class_id"+class_id);
        alert("section_id"+section_id);

        $.ajax({
            url: '<?php echo base_url(); ?>index.php?admin/get_class_section/' + day + section_id +  class_id,
           // alert("url"+url);
            success: function(response)
            {
                jQuery('#section_selection_holder').html(response);
            }
        });
    }

but when I try with a print_r in controller it shows (day12) both the day with both ids so the way pass data to the controller is correct?

Try this:

 $.ajax({
        url: '<?php echo base_url(); ?>index.php?admin/get_class_section/',
       data: {day: day, section_id: section_id, class_id: class_id},
       method: POST,
        success: function(response)
        {
            jQuery('#section_selection_holder').html(response);
        }
    });

and in your php controller you can get these values using $_POST. (print_r($_POST)) array.

use below code to call ajax with parameter

 function class_selection(day) {

    var section_id = document.getElementById('section_id2').value;
    var class_id = document.getElementById('class_id').value;
    $.ajax({
        type: "POST",
        dataType: 'json',
        data: 'day=' + day + '&sectionId=' section_id + '&classId=' + class_id,
        url: 'index.php?admin/get_class_section',
        success: function(json) {
          //your response here

        }

     });
}

you should also try to send data like below ajax call

var frm_data = {day: day,section_id:section_id,class_id:class_id}
$.ajax({
   url: '<?php echo base_url(); ?>index.php?admin/get_class_section/',
   type: 'POST',
   data: frm_data,
   dataType:'JSON',
   success: function(response)
   {
        jQuery('#section_selection_holder').html(response);
   }
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM