【问题标题】:How to get the POST data from a form inside of an iframe如何从 iframe 内的表单中获取 POST 数据
【发布时间】:2016-04-01 03:43:42
【问题描述】:

我的 CodeIgniter 应用程序的 iframe 中有一个表单:

<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>

提交此表单时,应执行以下代码(提交工作我测试过;我可以在此处获取表单的输入):

$(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();
        });

这是我的 ajax 控制器的 insertRole 函数:

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

这是我得到错误的地方:...index.php/ajax/insertRole 500 (Internal Server Error)

如何从 iframe 内的表单中检索 POST 数据?

【问题讨论】:

    标签: php jquery html ajax codeigniter


    【解决方案1】:

    如果您在捕捉“提交”事件时遇到问题,您可以使用这个:

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

    为了“等待”加载 iframe,然后捕获提交事件。您也可以在主文件中创建一个函数:

    function valueFromIframe(val) {
       alert(val)
    }
    

    并从 iframe 调用:

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

    希望对你有帮助

    【讨论】:

    • 这甚至没有回答我的问题。我的提交事件触发得很好。
    • 问题的名称:“如何从 iframe 中的表单获取 POST 数据”和您的最后一个问题:“如何从 iframe 中的表单中检索 POST 数据?”让我认为您无法从 iframe 中获取 $_POST ...如果您有 500 服务器错误,请尝试在 insertRole() 中回显 $_POST(以证明信息确实存在)并检查 $this-&gt;Database 是否已定义。通常我会尝试在单引号和双引号之间变化以避免出现问题(我的意思是:"&lt;?php echo base_url('index.php/ajax/insertRole'); ?&gt;"
    • 经过一些测试,我唯一的错误似乎是无效的 SQL 查询,但感谢您的帮助:)
    猜你喜欢
    • 2017-05-14
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2016-06-20
    相关资源
    最近更新 更多