AJAX jQUERY将POST中的数据传递给codeigniter Controller

时间:2016-02-27 07:14:50

标签: javascript php jquery ajax codeigniter

我想使用AJAX和Jquery在POST方法中将数据传递给我的控制器。

它看起来像这样:https://jsfiddle.net/10bhsd2o/ 我有自举下拉列表,显示数据库中的记录。

<li class="dropdown">


  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li ><a href="#"><?php

foreach($sites as $site)
{

echo "<li class='specialLink' id='$site->site_key'>".$site->site_key."</li>";
}?></a></li>

  </ul>
</li>

现在我想将数据发送到我的控制器,我从<li></li>标签下拉列表中选择的项目。

这是我的剧本:

   <script type="text/javascript">



    $( ".specialLink" ).click(function() {
        var value = this.id;

        var url= "<?php echo base_url('customer/dashboard/index') ?>"; 
        alert(url) 
         //get value for throw to controller
        alert(value);  

        $("#specialLink").submit(function(){
            $.ajax({ 
                    type: "POST", //send with post 
                    url: "<?php echo base_url('customer/dashboard/index') ?>", 
                    data: {value:value}, 
                    success:function(data){ 
                    alert(data) 
                    } 
              });
        });
    });


    </script>

HTML生成

<ul class="dropdown-menu">
    <li ><a href="#"><li class='specialLink' id='54th-65hy'>54th-65hy</li><li class='specialLink' id='HT45-YT6T'>HT45-YT6T</li></a></li>

  </ul>

控制器

public function index()
{

    var_dump($_POST);
    print_r($_POST);
    $this->data['subview'] = 'customer/dashboard/index';  
    $this->load->view('customer/_layout_main',$this->data);
}

现在我的警报是正确的我在警报中得到正确的网址我会在警报中获得正确的数据。

但是当我执行print_r($ _ POST)时,在我的控制器中;我得到空白数组为什么?我哪里错了?

3 个答案:

答案 0 :(得分:1)

你只需改变你的Javascript 您已删除$("#specialLink").submit(function(){});

 <script type="text/javascript">



$( ".specialLink" ).click(function() {
    var value = this.id;

    var url= "<?php echo base_url('customer/dashboard/index') ?>"; 
    alert(url) 
     //get value for throw to controller
    alert(value);  


        $.ajax({ 
                type: "POST", //send with post 
                url: "<?php echo base_url('customer/dashboard/index') ?>", 
                data: {value:value}, 
                success:function(data){ 
                alert(data) 
                } 
          });

});


</script>

答案 1 :(得分:1)

更改html标记。因为你在锚内部循环

    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <?php
          foreach($sites as $site)
          {
            echo "<li class='specialLink' id='$site->site_key'><a href='#'>".$site->site_key."</a></li>";
          }
        ?>
      </ul>
    </li>

然后更改您的click功能

    $( ".specialLink" ).click(function() {
        var value = this.id;
        var url= "<?php echo base_url('customer/dashboard/index') ?>"; 
        alert(url) 
         //get value for throw to controller
        alert(value); 
        $.ajax({ 
            type: "POST", //send with post 
            url: "<?php echo base_url('customer/dashboard/index') ?>", 
            data: {value:value}, 
            success:function(data){ 
            alert(data) 
            } 
        });
    });

我不知道您是否将index功能用于其他视图,但用于测试目的。先试试这个

    public function index()
    {
        if(!empty($_POST)){
            var_dump($_POST);
            print_r($_POST);
        }else{
            $this->data['subview'] = 'customer/dashboard/index';  
            $this->load->view('customer/_layout_main',$this->data);
        }
    }

让我知道你的第3次警报会得到什么

答案 2 :(得分:0)

$( ".specialLink" ).click(function() {
        var value = this.id;
        var url= "<?php echo base_url('customer/dashboard/index') ?>"; 
        alert(url) 
        alert(value); 
    $.post(url,{value:value},function(data){alert(data)});
  });