提交时关闭 iFrame fancybox 时出现问题
我有一个从数据库填充的在线用户名册。每个用户名都是一个链接,可在 fancybox (iframe) 中打开一个表单以允许编辑信息。子窗口中没有任何js,表单数据提交到数据库,但fancybox没有关闭。我需要它来提交数据,然后关闭 fancybox。
我已经尝试了这个问题<的Amr的答案< /a> 现在关闭但不提交。
这是打开 fancybox 的代码:
<script type="text/javascript">
$(function() {
$(".updateLink").fancybox({
'padding' : 0,
'autoScale' : false,
'transitionIn' : 'none',
'transitionOut' : 'none',
'width' : 650,
'height' : 560,
'showCloseButton': true,
'type': 'iframe'
});
});
</script>
<?php
foreach ($contacts as $contact) {
$updateLink = '<a class="updateLink" href="update_person.php?people_id='.urlencode($contact->people_id).'&company_id='.urlencode($company_id).'&uid='.urlencode($uid).' ">'.$contact->first_name.' '.$contact->last_name.'</a>';
print '<tr>';
print '<td>'.$contact->people_id.'</td>';
print '<td>'.$updateLink.'</td>';
print '<td>'.$contact->title.'</td>';
print '<td>'.$contact->email.'</td>';
print '</tr>';
} # end foreach contact
?>
这是 iframed 文件的代码现在的样子,带有关闭 fancybox 的函数:
<?php
include('/home/www/viola/ssl/include/ez_sql.php');
include('/home/www/lib/common_functions.php');
if (isset($_POST['submit'])) {
//write to roster table
$people_id = $_POST['people_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$title = $_POST['title'];
$email = $_POST['email'];
$db->query("INSERT INTO roster (people_id, first_name, last_name, title, email) values ($people_id, '$first_name', '$last_name', '$title', '$email')");
}
else {
$people_id = urldecode($_GET['people_id']);
$uid = urldecode($_GET['uid']);
$query = "SELECT id AS people_id, first_name, last_name, title, email
FROM new_people
WHERE active = 1 AND id = $people_id";
$person = $db->get_row($query);
$people_id = $person->people_id;
$first_name = $person->first_name;
$last_name = $person->last_name;
$email = $person->email;
$title = $person->title;
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<?= $jsPath; ?>/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript">
function closeME() {
event.preventDefault();
parent.$.fancybox.close();
$('#myForm').submit();
}
</script>
<style>
fieldset {margin: 35px;}
label {margin-right: 30px;}
input[type="text"]{
width:350px;
/*display:block;*/
}
input[type="button"]{
width:120px;
margin-left:35px;
display:block;
}
select {
width:350px;
}
</style>
<form action="update_person.php" method="post" id="myForm">
<input type="hidden" name="people_id" value="<?= $people_id; ?>"/>
<fieldset>
<legend>Update <?= $first_name . " " . $last_name; ?></legend>
<table>
<tr>
<td><label for="first_name">First Name:</label></td>
<td><input type="text" name="first_name" value="<?= $first_name; ?>"/></td>
</tr>
<tr>
<td><label for="last_name">Last Name:</label></td>
<td><input type="text" name="last_name" value="<?= $last_name; ?>"/></td>
</tr>
<tr>
<td><label for="user_email">Email:</label></td>
<td><input type="text" name="email" value="<?= $email; ?>"/></td>
</tr>
<tr>
<td><label for="title">Title:</label></td>
<td><input type="text" name="title" value="<?= $title; ?>"/></td>
</tr>
<tr><td colspan="2" style="text-align:center"><!--<input type="submit" id="mysubmit" name="submit" value="Submit changes" />-->
<button onclick="closeME();">
<span>Save changes</span>
</button>
</td></tr>
</table>
</fieldset>
</form>
<?
}
?>
在进行链接帖子中建议的更改之前,只有注释掉的 <; input type="submit" id="mysubmit" name="submit" value="提交更改" />
- 这确实正确提交了数据。
知道我需要做什么才能获得新代码来关闭 fancybox 并提交表单吗?
I have an online user roster populated from a database. Each username is a link that opens a form in a fancybox (iframe) to allow editing of info. Without any js in the child window, the form data submits to the database but the fancybox does not close. I need it to submit the data, then close the fancybox.
I've tried Amr's answer from this question and it now closes but doesn't submit.
Here's the code that opens the fancybox:
<script type="text/javascript">
$(function() {
$(".updateLink").fancybox({
'padding' : 0,
'autoScale' : false,
'transitionIn' : 'none',
'transitionOut' : 'none',
'width' : 650,
'height' : 560,
'showCloseButton': true,
'type': 'iframe'
});
});
</script>
<?php
foreach ($contacts as $contact) {
$updateLink = '<a class="updateLink" href="update_person.php?people_id='.urlencode($contact->people_id).'&company_id='.urlencode($company_id).'&uid='.urlencode($uid).' ">'.$contact->first_name.' '.$contact->last_name.'</a>';
print '<tr>';
print '<td>'.$contact->people_id.'</td>';
print '<td>'.$updateLink.'</td>';
print '<td>'.$contact->title.'</td>';
print '<td>'.$contact->email.'</td>';
print '</tr>';
} # end foreach contact
?>
Here's what the code of the iframed file looks like now, with the function that closes the fancybox:
<?php
include('/home/www/viola/ssl/include/ez_sql.php');
include('/home/www/lib/common_functions.php');
if (isset($_POST['submit'])) {
//write to roster table
$people_id = $_POST['people_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$title = $_POST['title'];
$email = $_POST['email'];
$db->query("INSERT INTO roster (people_id, first_name, last_name, title, email) values ($people_id, '$first_name', '$last_name', '$title', '$email')");
}
else {
$people_id = urldecode($_GET['people_id']);
$uid = urldecode($_GET['uid']);
$query = "SELECT id AS people_id, first_name, last_name, title, email
FROM new_people
WHERE active = 1 AND id = $people_id";
$person = $db->get_row($query);
$people_id = $person->people_id;
$first_name = $person->first_name;
$last_name = $person->last_name;
$email = $person->email;
$title = $person->title;
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<?= $jsPath; ?>/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript">
function closeME() {
event.preventDefault();
parent.$.fancybox.close();
$('#myForm').submit();
}
</script>
<style>
fieldset {margin: 35px;}
label {margin-right: 30px;}
input[type="text"]{
width:350px;
/*display:block;*/
}
input[type="button"]{
width:120px;
margin-left:35px;
display:block;
}
select {
width:350px;
}
</style>
<form action="update_person.php" method="post" id="myForm">
<input type="hidden" name="people_id" value="<?= $people_id; ?>"/>
<fieldset>
<legend>Update <?= $first_name . " " . $last_name; ?></legend>
<table>
<tr>
<td><label for="first_name">First Name:</label></td>
<td><input type="text" name="first_name" value="<?= $first_name; ?>"/></td>
</tr>
<tr>
<td><label for="last_name">Last Name:</label></td>
<td><input type="text" name="last_name" value="<?= $last_name; ?>"/></td>
</tr>
<tr>
<td><label for="user_email">Email:</label></td>
<td><input type="text" name="email" value="<?= $email; ?>"/></td>
</tr>
<tr>
<td><label for="title">Title:</label></td>
<td><input type="text" name="title" value="<?= $title; ?>"/></td>
</tr>
<tr><td colspan="2" style="text-align:center"><!--<input type="submit" id="mysubmit" name="submit" value="Submit changes" />-->
<button onclick="closeME();">
<span>Save changes</span>
</button>
</td></tr>
</table>
</fieldset>
</form>
<?
}
?>
Before making the change suggested in the linked post, there was just the commented-out <input type="submit" id="mysubmit" name="submit" value="Submit changes" />
- this did submit the data correctly.
Any idea what I need to do to get the new code to both close the fancybox AND submit the form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的代码存在以下问题之一:
解决方案
JS Fiddle 的工作代码 : http://jsfiddle.net/hMcc8/7/show/ (省略
/show/
以查看源代码)。必须添加/show/
才能使代码正常工作,以便 Same起源政策不干预。包含 fancybox JS 和 CSS 文件,并在主文件中定义以下函数:
框架文件的 Fiddle:http://jsfiddle.net/SnTwQ/7/
将
submit
事件处理程序绑定到您的表单,该处理程序调用parent.fancyBoxClose()
。使用submit
事件而不是按钮click
,因为可以通过在输入字段中按 Enter 来提交表单。它是如何工作的?
当提交表单时,调用父函数。此函数将
onload
事件处理程序附加到 Fancybox 框架。表单提交完成后,将加载新页面。当页面加载完成时,
onload
事件被触发。我们在此onload
事件中调用$.fancybox.close()
。结果,fancybox 按预期关闭。相关答案:未捕获的类型错误:无法读取未定义的属性“fancybox” - 仅 Google Chrome 错误?
Update: Another method. Fiddle: http://jsfiddle.net/hMcc8/9/show/.
According to the comments, the form is submitted to the same page. The following code should always work, when the pages are served at the same domain:
主要:
框架:
Your current code is having one of the following issues:
Solution
Working code at JS Fiddle : http://jsfiddle.net/hMcc8/7/show/ (omit
/show/
to see the source)./show/
has to be added to get the code work, so that the Same origin policy does not interfere.Include the fancybox JS and CSS files, and define the following function at your main file:
Fiddle for framed file: http://jsfiddle.net/SnTwQ/7/
Bind a
submit
event handler to your form, which callsparent.fancyBoxClose()
. Thesubmit
event is used instead of the buttonclick
, because it's possible to submit a form by pressing enter from within an input field.How does it work?
When the form is submitted, the parent function is called. This function attaches an
onload
event handler to the Fancybox frame. After the form has finished submitting, a new page is loaded.When the page finishes loading, the
onload
event is triggered. We're calling$.fancybox.close()
from within thisonload
event. As a result, the fancybox closes as expected.Related answer: Uncaught TypeError: Cannot read property 'fancybox' of undefined - Google Chrome only error?
Update: Another method. Fiddle: http://jsfiddle.net/hMcc8/9/show/.
According to the comments, the form is submitted to the same page. The following code should always work, when the pages are served at the same domain:
Main:
Frame: