1

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

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!

Community
  • 1
  • 1

2 Answers2

2

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. This allows you a lot of flexibility.

I would use the afterLoad() callback function within the Fancybox2 API.

See this fiddle i put together: Fancybox afterLoad() callback to return a function

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.

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

cerd
  • 2,171
  • 1
  • 18
  • 28
  • Thanks for your help, and your work. In principle, this seems to work, though I haven't been able to get it fully functional yet. – Imagesthataspire Dec 11 '13 at 00:44
  • What part are you still having trouble with- if need be I can show you some other code I have that will handle closing the box and then pushing an event back to parent window. This sounds fairly complex - you may want to look into angular.js - it has a learning curve - but event/view driven stuff is SO much easier. – cerd Dec 11 '13 at 01:51
  • 1
    The problem is content that has already been created. The result looks something like a Google search result (a search box followed by a list of links and descriptions with pagination at the bottom). So I can resubmit the search inside the iframe with your solution, but I can't seem to grab already generated content (ie. Next Page) and repopulate the iframe. I know its possible, I just haven't figured out how to call it with JS yet. Ill see if I can settup a fiddle to show you. – Imagesthataspire Dec 11 '13 at 14:14
0

try changing your javascript to use 'click' instead of 'on'. this should do it

jQuery(document).ready(function(submit) {
$('#check').click(function (e) {
e.preventDefault(); 
   $.ajax({
   ....
   }); 
 }); 
}); 
Jeribo
  • 465
  • 4
  • 9
  • if using jQuery 1.7+, you are just using a shortcode but nothing else. That doesn't solve the issue. – JFK Dec 11 '13 at 00:30
  • Unfortunately, JFK is correct. The problem is in the content constructed in fancybox. Some things need to be able to link out, like search results, while others need to effect the fancybox as a window. Thanks for you help though. – Imagesthataspire Dec 11 '13 at 00:41