Transfer data accurately from main window to fancybox iframe

Seeking assistance with a Wordpress plugin I've created using PHP. It's a gallery plugin that allows users to add captions and custom fields for images. The forms are displayed in a Fancybox modal, triggered by clicking input buttons.

Here is an example of the edit function:

$(".edit").click(function() {
    var formData = $(this).closest('form').serializeArray();
    formData.push({ name: this.name, value: this.value });

    $.fancybox.showActivity();

    $.ajax({
        type        : "POST",
        cache       : false,
        url         : "<?php echo plugins_url(); ?>/upload/editImageForm.php",
        data        : formData,
        success: function(data) {
            $.fancybox(data);
        }
    });
    return false;

});

The issue I'm facing is integrating the Edit form within an iframe, so that when the form is submitted, the action page appears inside the iframe instead of the main window. Currently, the action page loads in the main window causing exit from the WordPress admin area. How can I achieve this using an iframe for the Edit form and pass input values from the main window form to the fancybox-iframe with the edit form inside?

The HTML code for the form that opens the Fancybox looks like this:

<form method='post' action='' class="imageform">
    <input type='submit' value='Edit' name='edit' class="edit imageButton" />
    <input type='submit' value='Delete' name='delete' class="delete imageButton" onClick="return delete('<?php echo $image->name; ?>', '<?php echo $image->id; ?>')"/>
    <input name='imageID' type='hidden' value='<?php echo $image->id; ?>' class="imageID" />
    <input name='imageNavn' type='hidden' value='<?php echo $image->name; ?>' class="imageNavn" />
</form>

The Add Image form is simpler and functions correctly, displaying in the iframe as intended:

$("#upload").click(function() {
    $.fancybox.showActivity();

    $.fancybox({
        'type'              : 'iframe',
        'width'             : '50%',
        'height'            : '75%',
        'href'              : '<?php echo plugins_url(); ?>/upload/addImage.php'
    });

});

I hope you understand my dilemma and can provide guidance. Thank you in advance!

Answer №1

There doesn't appear to be a way to specify the name of the iframe that is created.

Below is the code that generates a fancybox iframe, showing that it uses the current time to ensure each created iframe is unique:

case 'iframe' :
    $('<iframe id="fancybox-frame" name="fancybox-frame' + new Date().getTime() + 
      '" frameborder="0" hspace="0" scrolling="' + selectedOpts.scrolling + 
      '" src="' + selectedOpts.href + '"></iframe>').appendTo(tmp);

I will need to write a patch (which should be relatively simple) for fancybox in order for our system to support this feature. Hopefully, someone else will do the same for the mainline fancybox code.

This feature was necessary for me because I perform file uploads in a fancybox and my Selenium testing environment requires knowledge of the iframe name in order to automate inputting test data in the correct window.

Answer №2

It's best to steer clear of iframes because they can be a hassle to work with. Instead, consider using ajax and json for improved results.

Why?

  1. Some users may have iframes disabled
  2. Cross-browser compatibility can be challenging
  3. Debugging and development can become inefficient due to constant switching between frames

If you absolutely must use iframes, you can change the iframe URL by using $('iframe').attr('src',url);. Another option is to utilize <a target="frame_name"> when setting up <iframe name="frame_name">

When working within the iframe itself, use either top.window or window.top (not entirely sure!) to reference the topmost window in the DOM, and self.window to refer to the iframe itself.

I hope this information proves useful in some way!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to create an auto-suggestion tool that can process and accept multiple input values, filtering out only those that begin with a designated character

I've been working on implementing an auto-suggestion feature that displays suggestions with partial matches or values within the input box. The goal is for it to only provide suggestions that begin with a specific character, like the "@" symbol. Fo ...

What is the best way to align these div elements within a table cell?

I am encountering an issue with the placement of elements. What I am striving for is something like this: https://i.stack.imgur.com/VSFXE.png where a div with several other divs inside is positioned at the top of the td, and another div is at the bottom o ...

Remove hidden data upon moving cursor over table rows after a delay

Hey there! I'm in need of a little assistance, so I hope you can lend me a hand. Here's the scenario: Within my category table, there are numerous rows, each containing a hidden textbox with an empty value and a unique ID. As users navigate t ...

Selected a radio button within a jQuery UI dialog box

After using jquery-ui, I was able to create a simple dialog window. It looks like this: <div id="dialog-form" title="Add Attribute Category"> <input type="radio" id="priceable" name="price" value="true" checked="checked"/> Priceable &l ...

What are the best techniques for using jQuery to manipulate an HTML form that contains nested elements?

I have a scenario where I need to dynamically generate mini-forms within an empty form based on certain conditions. For instance, imagine a form that gathers information about restaurants such as their names and locations. Depending on the number of restau ...

Using jQuery to search for corresponding JSON keys in the PokéAPI

Currently, in my development of an app, I am searching for and implementing an English translation of a language JSON endpoint using the PokéAPI. The challenge lies in identifying the correct location of the English language key within the array response, ...

Limiting the jQuery UI Datepicker to only allow past dates: How to make it happen?

I have implemented a jQuery UI Datepicker that restricts selection to only Sundays. However, I want to further enhance this by preventing users from selecting any dates in the future starting from today. Below is the current code snippet I am using for th ...

Calling Ajax in JavaScript

Trying to fetch a value in JavaScript using an Ajax Call, The code being used is as follows: <script> var value = $.ajax({ type:"GET", url:"get_result.php", data:"{'abc':" + $abc + "}", }); alert(val ...

Engage in a conversation with ASP.NET and SQL Server

I need to create a chat feature using asp.net. All the chat messages will be stored in a SQL Server database. Is there a way to display new messages from the database in real time? How can I get notified when another user sends a message? I have heard ...

Utilizing AJAX and PHP for dynamic changes based on listbox selection

Is there a way to make the query run whenever a selection is made from the listbox? if(isset($_POST['select'])){ $newselection=$_POST['select']; $query = "SELECT count(*) FROM listings where description=" . $newselection. ""; $r ...

Bending the rules for inventory levels in WooCommerce

Currently, I am directing users to a Contact 7 Form and adding items to the cart using a specific href link: http://example.com/checkout/?add-to-cart=1000 I am faced with a unique scenario where if there is only 1 item left of product 1000, and the user s ...

An effective way to determine the size of a browser through javascript

In an effort to enhance the responsiveness of a website, I have included the following code snippet on one of my pages: document.write(screen.width+'x'+screen.height); However, I am encountering an issue where the code displays my screen resolu ...

Stop Ajax requests when there are blank spaces in Typeahead.js

I've been experimenting with typeahead.js and utilizing the BloodHound remote feature to load data. Everything is functioning properly, except that when I input only spaces in the textbox, typeahead still makes an ajax call. I'm wondering if th ...

Change the color of a c3js chart when it loads

I have been searching for a way to customize the color of a scatter chart's plot, and I came across an example that uses d3 code within the chart http://jsfiddle.net/ot19Lyt8/9/ onmouseover: function (d) { d3.select(d3.selectAll("circle ...

Choose the tag and class then retrieve the custom attribute

I'm currently attempting to retrieve a specialized attribute utilizing jquery and subsequently choose it, nevertheless I am encountering some difficulties with the process Below is the jquery code I have implemented to access the value var stockId = ...

When I request the value of a JavaScript object, I am met with 'undefined'

I have been working on creating a Google map application that involves loading markers from a database and displaying them on a map. To accomplish this, I decided to create an array and define an object as shown below: function shop_info(name, latitude, l ...

How can we filter the input type file browse window to display only image files?

I'm trying to figure out how to filter the window popup so that it only shows image files when I click on the input type file. <input type="file" /> Any help would be greatly appreciated as I have been struggling to find a solution for this. ...

Is there a way to use JavaScript or jQuery to automatically convert HTML/CSS files into PDF documents?

While using OS X, I noticed that in Chrome I have the option to Save as PDF in the print window by setting the destination to "Save as PDF". Is this feature available on Windows? Is there a way to easily save to PDF with just one click? How can I access t ...

I'm having trouble getting my jsonp to function properly. Can anyone help me troubleshoot?

I've been working on my website for the past two weeks, learning HTML, CSS, and a bit of JavaScript. However, I'm facing an issue with the following code that I can't seem to resolve: <head> <script src="http://ajax.googleapis.com/ ...

Attempting to create a JavaScript function that will show a JSON array when the next button is clicked

I am facing an issue with displaying a JSON array based on specific start and end indices. Even though I managed to display the entire array, the first button click does not seem to work as expected. Upon clicking the first button, an error message "this.d ...