提交时关闭 iFrame fancybox 时出现问题

发布于 2024-12-11 14:16:44 字数 5320 浏览 0 评论 0原文

我有一个从数据库填充的在线用户名册。每个用户名都是一个链接,可在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-12-18 14:16:44

您当前的代码存在以下问题之一:

  • Fancybox 关闭,但表单未正确提交
  • 表单已正确提交,但 fancybox 不会关闭。

解决方案
JS Fiddle 的工作代码 : http://jsfiddle.net/hMcc8/7/show/ (省略/show/以查看源代码)。必须添加 /show/ 才能使代码正常工作,以便 Same起源政策不干预。

包含 fancybox JS 和 CSS 文件,并在主文件中定义以下函数:

function fancyBoxClose(){
    $("#fancybox-frame").load(function(){
        $.fancybox.close();
    });
}

框架文件的 Fiddle:http://jsfiddle.net/SnTwQ/7/
submit 事件处理程序绑定到您的表单,该处理程序调用 parent.fancyBoxClose()。使用 submit 事件而不是按钮 click,因为可以通过在输入字段中按 Enter 来提交表单。

$(function(){
    $("#myForm").submit(function(){
        parent.fancyBoxClose();
    });
});

它是如何工作的?
当提交表单时,调用父函数。此函数将 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:

主要:

$(function() {
    $(".updateLink").fancybox({
         'padding'        : 0,
         'autoScale'      : false,
         'transitionIn'   : 'none',
         'transitionOut'  : 'none',
         'width'          : 650,
         'height'         : 560,
         'showCloseButton': true,
         'type'           : 'iframe',
         'onClosed': function(){ /* Reset variable when closing*/
             fancyBoxToClose = false;
         }
     });
});
var fancyBoxToClose = false;
function fancyBoxLoaded(){
    if (fancyBoxToClose) {
       fancyBoxToClose = false;// Reset flag
       $.fancybox.close();     // Close fancybox
       return true;            // Callback for frame
    }
}
function fancyBoxClose(){
    fancyBoxToClose = true;    // Activate flag
}

框架:

// You don't need fancybox code at this page
$(function(){
    if(parent.fancyBoxLoaded()){ /* Notify that the DOM has finished*/
        // If the parent function returned true, this page is going to be closed.
        $('body').hide();  //Hide body, so that the user cannot submit another form.
    }
    else {
        $("#myForm").submit(function(){
            parent.fancyBoxClose();
        });
    }
});

Your current code is having one of the following issues:

  • Fancybox closes, but the form is not correctly submitted
  • The form is correctly submitted, but fancybox wont't close.

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:

function fancyBoxClose(){
    $("#fancybox-frame").load(function(){
        $.fancybox.close();
    });
}

Fiddle for framed file: http://jsfiddle.net/SnTwQ/7/
Bind a submit event handler to your form, which calls parent.fancyBoxClose(). The submit event is used instead of the button click, because it's possible to submit a form by pressing enter from within an input field.

$(function(){
    $("#myForm").submit(function(){
        parent.fancyBoxClose();
    });
});

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 this onload 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:

$(function() {
    $(".updateLink").fancybox({
         'padding'        : 0,
         'autoScale'      : false,
         'transitionIn'   : 'none',
         'transitionOut'  : 'none',
         'width'          : 650,
         'height'         : 560,
         'showCloseButton': true,
         'type'           : 'iframe',
         'onClosed': function(){ /* Reset variable when closing*/
             fancyBoxToClose = false;
         }
     });
});
var fancyBoxToClose = false;
function fancyBoxLoaded(){
    if (fancyBoxToClose) {
       fancyBoxToClose = false;// Reset flag
       $.fancybox.close();     // Close fancybox
       return true;            // Callback for frame
    }
}
function fancyBoxClose(){
    fancyBoxToClose = true;    // Activate flag
}

Frame:

// You don't need fancybox code at this page
$(function(){
    if(parent.fancyBoxLoaded()){ /* Notify that the DOM has finished*/
        // If the parent function returned true, this page is going to be closed.
        $('body').hide();  //Hide body, so that the user cannot submit another form.
    }
    else {
        $("#myForm").submit(function(){
            parent.fancyBoxClose();
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文