简体   繁体   English

在同一页面上处理来自ajax驱动的Fancybox元素的PHP请求

[英]Processing a PHP request from an ajax-driven Fancybox element on the same page

I've been working on a little project that involves collecting/processing search results and I thought you guys may be able to lend a hand. 我一直在从事一个涉及收集/处理搜索结果的小项目,我认为你们也许可以伸出援手。 I have a small script that takes a search value from a search input, processes it via PHP and then collects and inserts the results in a fancybox via JS. 我有一个小脚本,该脚本从搜索输入中获取搜索值,通过PHP处理它,然后通过JS收集结果并将其插入到花式框中。 Thus far, all is going well but I can't seem to work out the next bit. 到目前为止,一切进展顺利,但我似乎无法进一步解决。

I can't manage to interact with any elements in the fancy box because it will reload the page (for example, previous and next buttons or search input). 我无法与花式框中的任何元素进行交互,因为它将重新加载页面(例如,上一个和下一个按钮或搜索输入)。 How would you go about loading new content or form inputs into a fancybox on the same page instance using AJAX? 您将如何使用AJAX将新内容或表单输入加载到同一页面实例上的fancybox中?

HTML: HTML:

<form action="search.php" method="post" name="search" id="search">
<input size="35" value="" placeholder="Search" name="search" id="result" autocomplete="off">
<button id="check" data-fancybox-type="ajax" href="search/search.php" id="check">Search</button> 

Script: 脚本:

jQuery(document).ready(function(submit) {
$('#check').on("click", function (e) {
    e.preventDefault(); 
    $.ajax({
        type: "POST",
        cache: false,
        url: "search.php",
        data: $("#result").serializeArray(), 
        success: function (data) {
        // on success, post returned data in fancybox
        $.fancybox(data, {
            // fancybox API options
            fitToView: true,
            openEffect: 'fade',
            closeEffect: 'fade'
        }); 
        } 
    }); 
}); 
}); 

The above script is largely basses on this post 上面的脚本在这篇文章上基本上是低音

Thanks for your ideas! 感谢您的想法!

The fancybox API provides a number of event based callback functions for interacting with the fancybox elements, before, during and after the box is shown or loaded . fancybox API提供了许多基于事件的回调函数,用于在shownloaded框之前,期间和之后与fancybox元素进行交互。 This allows you a lot of flexibility. 这给您很大的灵活性。

I would use the afterLoad() callback function within the Fancybox2 API . 我将在Fancybox2 API中使用afterLoad()回调函数。

See this fiddle i put together: Fancybox afterLoad() callback to return a function 参见我整理的这个小提琴: Fancybox afterLoad()回调以返回函数

This is just my solution, I am still very much a student of js, so I can't say this is the best way, and I welcome feedback or edits. 这只是我的解决方案,我仍然是js的学生,所以我不能说这是best方法,欢迎您提出反馈或进行修改。

Basically we will use the afterLoad() API function of fancybox so that upon successful load of the fancybox element driven from your successful $.ajax call a function is returned to then listen to the click event of an element loaded into your fancybox . 基本上,我们将使用fancybox的afterLoad() API函数,以便在成功加载成功$.ajax调用驱动的fancybox元素后,将返回一个函数,然后监听加载到fancybox中的元素的click事件

$.fancybox(echoData, {
                    // fancybox API options
                    fitToView: true,
                    openEffect: 'fade',
                    closeEffect: 'fade',
                    afterLoad: function () {
                        return fancyUpdateMyWay = function () {
                            var a = Math.random();
                            var newHtml = "<h4>UPDATED=" + a + "</h4>";
                            $("#fancyResult").html(newHtml);

                        }

                    }

                });

Note: I imagine that you will want to do further ajax calls from within the fancybox results, and here I only demonstrated a simple DOM update from jquery. 注意:我想您会希望从fancybox结果中进行进一步的ajax调用,而在这里,我仅演示了jquery的简单DOM更新。

try changing your javascript to use 'click' instead of 'on'. 尝试将您的JavaScript更改为使用“点击”而不是“开启”。 this should do it 这应该做

jQuery(document).ready(function(submit) {
$('#check').click(function (e) {
e.preventDefault(); 
   $.ajax({
   ....
   }); 
 }); 
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM