[英]How to get the POST data from a form inside of an iframe
I have a form inside of an iframe in my CodeIgniter application: 我在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>
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: 这是我的ajax控制器的insertRole函数:
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)
这是我得到错误的地方: ...index.php/ajax/insertRole 500 (Internal Server Error)
How can I retrieve the POST data from a form inside an iframe? 如何从iframe中的表单中检索POST数据?
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. 为了“等待”正在加载的iframe,然后捕获Submit事件。 You also could create a function in the main file: 您还可以在主文件中创建一个函数:
function valueFromIframe(val) {
alert(val)
}
And call from the iframe: 并从iframe调用:
<script type="text/javascript">
parent.valueFromIframe("Hello world");
</script>
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.