【问题标题】:Adding records to a drop down menu without form refresh在不刷新表单的情况下将记录添加到下拉菜单
【发布时间】:2015-01-20 07:46:35
【问题描述】:

我想在不刷新表单的情况下将记录添加到下拉菜单。我正在使用 codeigniter 和 bootstrap

这是引导模式:

<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
            <h4 id="myLargeModalLabel" class="modal-title">Add Record</h4>
        </div>
        <div class="modal-body">

            <form class="sky-form" id="sky-inchidere" method="post" accept-charset="utf-8" action="">
                    <dl class="dl-horizontal">

                        <dt>Name<span class="color-red">*</span></dt>
                        <dd>
                            <section>
                                <label class="input">
                                    <i class="icon-append fa fa-inbox"></i>
                                    <input type="text" value="" name="name" required>
                                    <b class="tooltip tooltip-bottom-right">Add New Record</b>
                                </label>
                            </section>
                        </dd>

                    </dl>
                <hr>
                <button type="submit" class="btn-u" style="float:right; margin-top:-5px;">Submit</button>
            </form>
        </div>
    </div>
</div>

Ajax 脚本:

$(document).ready(function(){

$("#sky-inchidere").submit(function(e){     
    e.preventDefault();
    var tdata= $("#sky-inchidere").serializeArray();
    $.ajax({
        type: "POST",
        url: 'http://localhost/new/oportunitati/add',
        data: tdata, 

        success:function(tdata)
        {
            alert('SUCCESS!!');
        },
        error: function (XHR, status, response) {
           alert('fail');
        }
    });
});
});

CI 控制器(我在这里添加了模态代码进行测试)

public function add()   {
    $tdata = array( name=>  $this->input->post(name),
                   );
    $this->db->insert('table',$tdata); 
}

当我使用此代码时,我收到“失败”错误消息。

感谢您的宝贵时间。

【问题讨论】:

    标签: ajax twitter-bootstrap codeigniter


    【解决方案1】:

    如何调试: 1. 打印你的 'tdata' 看看会发生什么; 2.这里出了点问题:$this->input->post('name');

    【讨论】:

    • 试图在控制台打印'tdata',看看里面有什么?
    【解决方案2】:

    尝试使用:

    $tdata = array( 
        'name' =>  $this->input->post('name')
    );
    

    【讨论】:

      【解决方案3】:

      我设法找到问题并纠正它。 (表名上的错字) 现在我遇到了一个不同的问题。在 ajax 成功中,我无法刷新我尝试过的所选下拉记录:

             success:function(tdata)
             {
                 // close the modal
                 $('#myModal').modal('hide');
      
                 // update dropdown ??
                 $('.chosen-select').trigger('liszt:updated');
                 $('#field-beneficiar_p').trigger('chosen:updated');
                 $('#field-beneficiar_p').trigger('liszt:updated');
             },
      

      任何帮助我如何刷新记录以查看最后添加的项目将不胜感激。我正在使用选择的插件。

      【讨论】:

        【解决方案4】:

        从控制器发送数据到json_encode 然后在js函数中

        $.ajax({
                            type: "POST",
                            url: "<?php echo base_url('login/get_time'); ?>",
                            data: {"consltant_name": consltant_name, "time": time},
                            success: function(data) {
                                var data = JSON.parse(data);
                                var options = '';
                                options = options + '<option value="">Please Select One</option>'
                                $.each(data, function(i, item) {
                                    options = options + '<option value="' + item + '">' + item + '</option>'
                                });
                                selectbox.html(options);
                            }});
        

        【讨论】:

        • 对不起,我不明白如何修改我的代码来解决我的问题。有没有办法提供更详细的例子?
        猜你喜欢
        • 1970-01-01
        • 2015-09-30
        • 1970-01-01
        • 2019-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多