doume1301 2016-03-31 19:25
浏览 179

如何从iframe内的表单中获取POST数据

I have a form inside of an iframe in my CodeIgniter application:

<div class="settings"><h2>Einstellungen</h2>
  <div class="main-content">
    <iframe src="" name="main-content">
      <form id="new-role-form" method="post" action="<?php echo base_url('index.php/home/settings') ?>">
        <input class="role-input" type="text" placeholder="Rolle" name="role"><input class="role-input" type="password" placeholder="Passwort" name="password"><button class="button role-submit" type="submit"><span class="glyphicon glyphicon-plus"></span></button>
      </form>
    </iframe>
  </div>
</div>

When submitting this form the following code should be executed (the submit is working I tested it; I can get the inputs of the form here):

$(document).on('submit', '#new-role-form', function(event) {
            var form = $(this);
            var name = $('input[name="role"]', form).val();
            var password = $('input[name="password"]', form).val();
            $.ajax({
                url: '<?php echo base_url('index.php/ajax/insertRole'); ?>',
                type: 'POST',
                data: {name: name, password: password},
                success: function() {
                    alert("success");
                }
            });
            event.preventDefault();
        });

And this is the insertRole function of my ajax controller:

public function insertRole() {
        $this->Database->insert($this->input->post('name'), $this->input->post('password'));
    }

This is where I get the error: ...index.php/ajax/insertRole 500 (Internal Server Error)

How can I retrieve the POST data from a form inside an iframe?

  • 写回答

1条回答 默认 最新

  • dourukeng5302 2016-03-31 19:38
    关注

    If you have problems catching the 'submit' event you could use this:

    <script type="text/javascript">
        $('#frame').load(function() {
            $(this).contents().find('form').submit(function() {
                //do your stuff here
            });
        });
    </script>
    

    In order to 'wait' for the iframe being loaded and then catch the submit event. You also could create a function in the main file:

    function valueFromIframe(val) {
       alert(val)
    }
    

    And call from the iframe:

    <script type="text/javascript">
     parent.valueFromIframe("Hello world");
    </script>
    

    Hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮