【问题标题】:How to submit Form by Bootstrap Modal using Ajax如何使用 Ajax 通过 Bootstrap Modal 提交表单
【发布时间】:2013-04-08 08:48:34
【问题描述】:

我会尽量简洁,我是引导程序和高级 javascript 的新手,基本上我使用引导程序模式在用户面前加载表单以收集和保存数据库中的数据。我的问题是你将如何使用 ajax 来做到这一点,因为我还有其他模式也会有不同的形式,我不想一次又一次地处理整个页面。 附言 我正在codeigniter中制作这个应用程序

这是modal里面的表格

 <section id="launch" class="modal hide fade" style="width: 800px; left: 500px;">
              <header class="modal-header" style="height: 7px; background: none repeat scroll 0% 0% rgb(87, 164, 210);">
                <h4 style="margin-bottom: 0px; margin-top: -5px; text-align: center; color: white;">Unit Details</h4>
                <button type="button" class="close" data-dismiss="modal" style="margin-top: -20px;">&times</button>
              </header>
 <?php echo form_open('setupnewblock/registerNewBlock'); ?>
<div class="modal-body" style="padding-top: 0px; padding-left: 0px; " >
 <div style="position: relative; left: 43px; margin-top: 41px; margin-left: 0px;">
                <span>Address 1</span>
                <input  type="text" placeHolder="" name="address1" style="width: 219px; height: 17px; margin-left: 20px; margin-top: 3px;" value="<?php echo set_value('address1'); ?>"/>
                <br />
                <span>Address 2</span>
                <input  type="text" placeHolder="" name="address2" style="width: 219px; height: 17px; margin-left:20px; margin-top: 3px;" value="<?php echo set_value('address2'); ?>"/>
                <br />
                <span>Address 3</span>
                <input  type="text" placeHolder="" name="address3" style="width: 219px; height: 17px; margin-left:20px; margin-top: 3px;" value="<?php echo set_value('address3'); ?>"/>
                <br />
                <br />
              </div>
 <footer class="modal-footer" style="margin-top: 475px;">
          <input type="submit" value="Save" name="save" class="btn btn-primary" style="margin-top: 2px;  height: 27px; width: 89px;" data-dismiss="modal"></input>
          <?php echo form_close(); ?>
        </footer>
      </section>

我想使用 ajax 提交这个表单

【问题讨论】:

标签: javascript html codeigniter twitter-bootstrap


【解决方案1】:

我也遇到了同样的问题,但是当我尝试这个gist 时,一切都做得很好。

你可以试试这个代码:

$(document).ready( function() {
    $('.modal form').on('submit', function(event) {
        event.preventDefault()
        $.post( $(this).attr('action'), $(this).serialize(), function(data) {
            // just try to see the outputs
            console.log(data)
            if(data.return===true) {
                // Success code here
            } else {
                // Error code here
            }
        }, 'json')
    })
});

【讨论】:

    【解决方案2】:

    如果你想做的很简单,这应该适合你:

    $('input[name=save]').click(function(e) {
        e.preventDefault();
    
        $.post("setupnewblock/registerNewBlock", {form: $("#launch form").serialize()},  
            function(data) {
            // callback   
        });
    });
    

    【讨论】:

      【解决方案3】:

      看看 codeigniter user guide

      所以你的代码应该是,

      $attributes = array('class' => 'myform', 'id' => 'myform');
          echo form_open('setupnewblock/registerNewBlock', $attributes);
      

      然后,您必须将表单的提交功能绑定到 jquery 功能,例如

      $(document).ready(function() {
          $("#myform").submit(function() {
      
              $.ajax(
                      {
                          type: 'POST',
                          url: 'your url to php file',
                          data: {}, //your form datas to post          
                          success: function(response)
                          {
                              alert(response);
      
                          },
                          error: function()
                          {
                              alert("Failure");
                          }
                      });
      
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2017-12-20
        • 1970-01-01
        • 1970-01-01
        • 2014-05-14
        • 2022-01-02
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多