Retrieving information from an iframe using JavaScript

I am working on a simple web page that includes an iframe. Within this iframe, there are three input fields that users need to fill in. I am looking for a way to retrieve the data entered by the user in each of these input fields using JavaScript.

Below is the JavaScript code snippet:

<script type="text/javascript"> var ticket = window.frames[0].document.getElementById('ticket').ticket; alert(ticket); </script>

Inside the iframe, I have the following input field:

<input type='text' name='ticket' id='ticket'...

Despite entering data into all 3 input fields and clicking 'ok', nothing seems to happen. I am now wondering how I can save the data entered in these input fields to a .txt file so that I can later retrieve this text using PHP and store it in a database.

Answer №1

I'm not sure about the accessibility of iframes through the window.frames property. A potential alternative approach could be:

const frame = document.querySelectorAll("iframe")[0]
  , formData = new FormData(frame.contentDocument.forms[0]);
console.log("Success: data=" + formData.toString());

Dealing with storing form values in a database presents its own challenges. One possible solution might involve bypassing JavaScript and configuring the iframe's form to submit a POST request to your PHP handler for storage purposes.

Answer №2

Spending a significant amount of time, I tried to solve the puzzle of exchanging data between text boxes in two Iframes that I authored. Despite much wasted effort and scouring the internet for solutions, the answer turned out to be surprisingly simple. It seems like everyone online is tackling much more complex challenges. For those seeking a straightforward solution!

In my setup, there is a main page with two Iframes (ID = ifr1 and ifr2). Each frame contains a text box (ID = tb1 in ifr1). To retrieve the contents of tb1 in iframe ifr2, all you need to do in JavaScript is:

parent.ifr1.tb1.value or

parent.ifr1.document.getElementByID(‘tb1’).value.

To modify the value, simply use:

parent.ifr1.tb1.value=”whatever”
or
parent.ifr1.document.getElementByID(‘tb1’).value=”whatever”

You can also access a variable from ifr1 in ifr2 by using:

parent.ifr1.var_in_ifr1 where var_in_ifr1 is defined in the script of ifr1

var var_in_ifr1=”whatever”

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

Using Vuejs 2 to manage assets

As a beginner in VueJS, I am exploring ways to incorporate an external JavaScript library into my component. After downloading the minified JS file and placing it in my assets directory, I'm unsure how to compile my component to utilize this library ...

Having issues with the custom listing feature in Jquery UI auto complete not functioning as expected

I am facing an issue with my implementation of jquery UI autocomplete. The autocomplete functionality is not working as expected, despite the absence of any JavaScript errors. Below is the JSON data that I am working with: {"d":"[{\"label\":&bs ...

How can you determine if multiple checkboxes have been checked with "if" statements following a button click?

I am looking to display a message after clicking a button if one or more checkboxes are checked. I would prefer using Jquery for this functionality. Any help on achieving this would be highly appreciated. $(function() { $(".btn").click(function() { ...

The jQuery click event for sending data is not functioning properly, as it only updates the existing

I am currently working on implementing a click send function for my emoticon feature but I'm facing some issues with it. You can view the interactive demo I created on JSFiddle. The demo features four text areas and emoticons. When you click on an em ...

Utilize numerous instances of redux-form on a single webpage and submit them all at once with a

Currently, I am utilizing redux-form within a Modal that contains 'Validate' and 'Cancel' buttons. I have three distinct forms, each of which will generate three separate entities in the backend. My aim is to produce and dispatch two ...

Imitate a HTTP request

Currently, I am working on developing a front-end application using Angular (although not crucial to this question). I have a service set up that currently supplies hard-coded JSON data. import { Injectable } from '@angular/core'; import { Obser ...

Unraveling the Mystery of @Input and @Output Aliases in Angular 2

After researching about the @Input() and @Output() decorators, I discovered that we have the option to use an alias instead of the property name for these decorators. For example: class ProductImage { //Aliased @Input('myProduct') pro ...

Creating a set of labels using values derived from a collection of objects

Looking for advice on how to loop through a Serialized JSON string using an ajax call in JavaScript. I have 8 labels set up and want to assign each key and value from the JSON string to a different label. Any guidance or feedback is appreciated! I'm ...

Acquire information from JSON API encoded child rows

When using redux, I am receiving the API structure like this: { "wrapper": { "db": [ { "payload": "7b226465766963654964223a224d7956656869636c65222c226576656e7444617461223a7b2273746174697374696373223a7b227370656564223a37372c2272706d ...

JavaScript is causing a performance problem when attempting to hide the header while scrolling

In my attempt to create a navigation bar, I have specific requirements: It should start with a large height Disappear when scrolled down Show a smaller version when scrolling back up Expand to full height when fully scrolled to the top I have managed to ...

What is the process for creating a duplicate element within the target div after the original element has been dropped?

Imagine a scenario where the "HI" div is dragged into the "DRAG HERE" div, causing the "HI" div to be removed from its original location. What if, instead of disappearing, dragging the "HI" div to another location generated a new "HI" div? /** * Fu ...

Trouble with AJAX request on iPad, works perfectly on desktop

Here is some of my custom plugin code (function ($) { $.fn.createGallery = function(options) { var theObject = $(this); var settings = $.extend({ // Default settings. server: 'http://localhost/jQuery%20Gall ...

Are you familiar with a cutting-edge HTML5-powered, heritage-defying JavaScript framework?

Which cutting-edge framework should I choose to fully utilize the latest HTML5 technologies provided by modern browsers like Firefox 7, Safari 5, and Chrome 14? I have no need to support any older browsers such as IE, Firefox, or Chrome versions prior to t ...

JavaScript Function is having issues when dealing with whole numbers

When it comes to my JavaScript function designed to inquire about the number of products a user wants to order, I'm facing some issues. The function should display a message if the user tries to order less than one product and show an alert saying "Or ...

Concealing the Footer on Mobile Websites in Muse UI When the Keyboard Appears

In this project, I am using vue.js along with muse.ui and only incorporating JavaScript and CSS without the jQuery library. Currently, the footer position always ends up on top of the keyboard whenever an input field is focused. Is there a way to ensure th ...

Unable to retrieve the string contained within an element - JavaScript object literal

I am currently facing an issue where I am attempting to retrieve the text content of two div elements with classes .class-date and .class-time, but I keep encountering an Uncaught TypeError stating "e.siblings is not a function". I believe this may be a ...

Using jQuery to display the first list item following the last list item

Is there a way to remove the first list item and add it after the last list item using jQuery? Here is the HTML code: <ul id="client_list"> <li id="1"><img src="img/client.png"/>1</li> <li id="2"><img src="img/clie ...

Tips for adding a child div to encase the inner content

Can someone help me figure out how to use pure JS (not jquery) to add a <del> inside the <div> that contains the text 'abc'? For practice purposes, I am not allowed to use innerHTML method, inline CSS, or jquery. Just plain JS. <h ...

Convert your pandas data to JavaScript using the power of bootstable

I have a question that may seem basic, as I am new to web development. I have been looking into the best ways to display pandas data on an HTML page and manipulate it using pure JavaScript (not jQuery). I have explored several open-source options like (h ...

How to pass an item as a parameter to a computed property in Vue.js, and have it return a sorted child array within a

After spending some time searching on Google, I am still struggling to find a solution for this issue. I have a list of "Intents" that contain nested lists of "Entities" created using v-for loops. The Intents are already computed, but now I need to dynam ...