简体   繁体   English

如何使用Bootstrap在CodeIgniter中将数据从下拉菜单传递到PHP

[英]How to pass data from dropdown menu to PHP in CodeIgniter with Bootstrap

I am building an application with Codeigniter and Bootstrap. 我正在使用Codeigniter和Bootstrap构建应用程序。

What I want to do is to have a dropdown menu which can change a status in sql , and then in background via AJAX call PHP script to do that and display a confirmation message - with bootstrap alert. 我想要做的是拥有一个下拉菜单,该菜单可以更改sql中的状态 ,然后通过AJAX在后台调用PHP脚本来执行此操作并显示确认消息 -带有启动警报。 Everything should be done without page refresh. 无需刷新页面即可完成所有操作。

The problem is that I can't find a solution to pass the variable from drop down to PHP without page refresh via POST. 问题在于,如果没有通过POST刷新页面,我找不到将变量从下拉列表传递到PHP的解决方案。

I am trying to do something like this: 我正在尝试做这样的事情:

<!-- AJAX which hide the DIV -->
<script>
    $(document).ready(function(){
      $("#message").hide();
       $('#status li').click(function () {
            $("#message").show();
            $("#success-alert").load('<?php echo base_url() ?>/changestatus/150/', {my_var : $("#my_var").val()});
            $("#message").fadeTo(2000, 500).slideUp(500, function(){
               $("#message").hide();
            });
        });
    });
</script>

In the above code, I would like to have a link like: /changestatus/150/2 where 150 is a sample lead id, and 2 is a new status choosen from dropdown. 在上面的代码中,我想要一个链接,例如:/ changestatus / 150/2,其中150是示例销售线索ID,2是从下拉列表中选择的新状态。

<!-- Alert DIV, which is hidden on load -->
<div id="message">
    <div class="alert alert-success" id="success-alert">
        <button type="button" class="close" data-dismiss="alert">x</button>
        <strong>OK! </strong>
        The changes has been done :-)
    </div>
</div>


<!-- Dropdown menu -->
<div class="btn-group" id="myDropdown">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    Menu
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" id="status">
    <li><a href="#">Chg status to 1</a></li>
    <li><a href="#">Chg status to 2</a></li>
    <li><a href="#">Chg status to 3</a></li>
  </ul>
</div>

In the above drop down, I do not know where to put the ID numbers 1,2,3. 在上面的下拉列表中,我不知道将ID号1,2,3放在哪里。 and how to send it as my_var to AJAX when user clicks option ..and function in my controller 以及如何在用户单击我的控制器中的选项..and函数时将其作为my_var发送到AJAX

<?
    public function changestatus($lead_id)
    {
        //read a new status from segment in link...
        $new_status = $this->uri->segment(4),

        //...or from POST
        if(isset($_POST['my_var']))
        {
            $new_status = $_POST['my_var'];
        }

        // then will be some call to model which mades changes to DB

        echo '...done';
    }
?>

I've spent a whole day trying to do without success, please help if you can :) 我花了整整一天的时间尝试未成功,如果可以的话,请帮助:)

You can make an extra attribute to all your li and assign them values. 您可以为所有li一个额外的属性,并为其分配值。 Get those data attributes in your jQuery code and pass it to the ajax request page. 在您的jQuery代码中获取那些数据属性,并将其传递给ajax请求页面。

<ul class="dropdown-menu" id="status">
    <li data="1"><a href="#">Chg status to 1</a></li>
    <li data="2"><a href="#">Chg status to 2</a></li>
    <li data="3"><a href="#">Chg status to 3</a></li>
  </ul>

Change jQuery code as below: 更改jQuery代码,如下所示:

$('#status li').click(function () {
            var my_var = $(this).attr('data'); //Use this value for your page
            $("#message").show();
            $("#success-alert").load('<?php echo base_url() ?>/changestatus/150/', {my_var : my_var});
            $("#message").fadeTo(2000, 500).slideUp(500, function(){
               $("#message").hide();
            });
    });

Instead of using $("#success-alert").load() you should use $.post to send post data ( enter link description here ). 代替使用$(“#success-alert”)。load(),您应该使用$ .post发送帖子数据( 在此处输入链接描述 )。 $.load uses GET to fetch data from web server. $ .load使用GET从Web服务器获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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