在CodeIgniter中将选定的值从JavaScript发送到Controller?

时间:2017-11-17 11:45:54

标签: javascript php ajax codeigniter

这是我想从中发送数据的视图。当我选择类名时我想通过 Ajax

将其发送到控制器
<app-route
route="{{route}}"
pattern="/users/:id"
data="{{usersRouteData}}"
tail="{{usersSubroute}}"
active="{{usersActive}}"
></app-route>

这是我的 ajax 功能:

<select class="form-control" name="persent_class" id="class">
<?php foreach ($all_classes as $class) : ?>
  <option value="<?php echo $class['class_name']?>"><?php echo 
  $class['class_name']?></option>
  <?php endforeach; ?>
          </select>

这是我的控制器功能

<script type="text/javascript">
    $(document).ready(function () {
        $("#class").change(function () {
            var c = $("#class").val();
            $.ajax({
                type: "POST",
                url: "<?=base_url()?>index.php/Welcome/get_class_name",
                data: {"c":c},
                dataType:"json",
                cache: "false",

            });
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

尝试使用此脚本,更改某些行并忘记echo base_url:

<script type="text/javascript">
    $(document).ready(function () {
        $("#class").change(function () {
            var optionSelected = $(this).find("option:selected");
            var valueSelected  = optionSelected.val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>index.php/Welcome/get_class_name",
                data: {"c":valueSelected},
                dataType:"json",
                cache: "false",

            });
        });
    });
</script>