Is it possible for an image to be uploaded through the Shadowbox upload field and then submit it once the

On the main page, there is a form that contains a button to trigger a Shadowbox child form.

The Shadowbox child form has its own form, and when submitted, it fills in hidden data in the parent form. Dealing with non-image data is straightforward.

But how can one manage image uploads? Specifically, how can I pass an image from the Shadowbox form to the parent form, and then upload it after the parent form is submitted?

I selected these tags based on the languages I am utilizing for this project.

Answer №1

I discovered a great solution for this issue by referring to this helpful answer, after doing some extensive research.

The gist of it involves using an onchange event on the file input field to store the base64 encoded version of the selected image in a variable on the parent page.

Here is the code snippet for the shadowbox page:

// Javascript
$('[type=file]').change(function ()
{
    if (input.files && input.files[0])
    {
        var reader = new FileReader();

        reader.onload = function (e)
        {
            // The encoded image is saved in the parent variable here.
            self.parent.hiddenElement.val(e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

And here's the code snippet for the parent page:

// HTML
<input type='hidden' name='encoded_image' />

// Javascript
var hiddenElement = $('[name=encoded_image]');

Now, you can easily save this base64 encoded image to blob storage on your server upon submitting the form on the parent page.

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

Testing Vue with Jest - Unable to test the window.scrollTo function

Is there a way to improve test coverage for a simple scroll to element function using getBoundingClientRect and window.scrollTo? Currently, the Jest tests only provide 100% branch coverage, with all other areas at 0. Function that needs testing: export de ...

How can you transform a string into an array using JavaScript?

Looking to transform a string into an array using javascript? var strng = "[x,y]" The desired result should be: var arry = ["x","y"] ...

Content in TinyMCE styled with the default CSS of the application

Hello fellow developers; I'm struggling to find a suitable solution to apply the same styles from our main CSS file to the text entered in the TinyMCE editor. Just to clarify, I don't want to alter the overall theme or appearance of TinyMCE itse ...

Guidelines on programmatically adding a directive to a div element in AngularJS

var myApp = angular.module('myApp', []); myApp.controller('someController', function($scope) { $scope.click = function(){ alert("Hello there!"); }; }); myApp.directive('testInput',function(){ return { ...

Searching for a value in Mongodb using a dynamic key with Node.js

I have been attempting to retrieve a value associated with a key in MongoDB, but so far I have been unsuccessful. Here is an example of my output: { "_id" : { "$oid" : "52cfc91adbffcbe08ccf94b0"} , "customerInfo" : "value"} For instance, if the user inpu ...

What specific data type is required for the following code in Angular?

const componentMapper = { input: InputComponent, button: ButtonComponent, select: SelectComponent, date: DateComponent, radiobutton: RadiobuttonComponent, checkbox: CheckboxComponent, switch: SwitchComponent, textarea: TextAreaComponent }; ...

Fetching post data from an HTML form in Node.js using Express 4: A comprehensive guide

This is the content of my main.js file: var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var userAPI = express.Router(); app.use(express.static(__dirname + '/public')); app.use( bodyParser.json() ) ...

Steps for Successfully Submitting Data from Vue Component to Laravel Controller

Currently, I am utilizing Laravel, Blade, and Vue.js. My goal is to send a prop from a Vue.js component along with all the other form data. However, I am facing an issue because the main part of the form resides in Blade while the prop containing an array ...

Add letters to the input field as soon as typing begins without the need to first click on the input field when

Take a look at the code below: document.addEventListener('keyup', logKey); function logKey($event) { var charCode = $event.keyCode; if(charCode > 31 && (charCode < 48 || charCode > 57 || charCode > 107 || charCode > ...

TypeScript definition for a combination of letters, numbers, and symbols

Can you help me define a type for a response that has this format? TYGOokcgyA9-FQZPM7-evpely6ETEnLyU2yq6hTD_XpTWkPckEP5bFm79hUTtE7rpa6Aiqc6s7xcTXQNNLSClTWtmc7uMIhf-44r3W3d7qY_LkhkGKuv In Typescript, what type should I use for this structure? export interf ...

Is there a way to retrieve the value of a select element that has more than one option selected?

On my website, there is a select element that enables users to choose multiple options. My goal is to gather an array containing the values of all currently selected options. I attempted to utilize the selectedOptions property of the select element in ord ...

Solving data within an Angular Controller

Recently delving into Angular development, I've found myself caught in a loop trying to solve this particular issue. To give some context, I'm utilizing the MEAN stack through mean.io which includes Angular UI Router functionalities. In my data ...

I would like to inquire about the process of accessing profile information on a website through the LinkedIn API

Do you know how I can use the latest LinkedIn JavaScript API with OAuth 2.0 to retrieve my own profile details for a website? The goal is to automatically update the website using my linked profile information. I have attempted the following: api_key: ...

Viewing the photo container before uploading while having text overlap

I'm encountering an issue where the image previews are overlapping with the text in another div. Here are the screenshots: the first one shows how it looks before the preview, and the second one shows what happens when images are added: https://i.sst ...

Creating custom events in a JavaScript constructor function

Just beginning to learn JavaScript. I have a custom function that I want to assign events to in the constructor. var myFunction = function(){ /* some code */ } myFunction.prototype.add=function(){ /* adding item*/ } Now I am looking to add an event to ...

How to utilize Vue.js method to retrieve child prop?

In my Vue.js project, I have created two components. The main component uses a child component called NoteRenderer, which includes a prop named data_exchange. My goal is to update this prop from the main component when a button is clicked. I attempted to a ...

Integrating React js with Layout.cshtml: Mastering the Fusion of React Routing and ASP.NET MVC Routing

My current project involves an ASP.NET MVC core application with the View written in cshtml. The routing follows the conventional asp.net mvc routing pattern. However, I've recently implemented a new module using React JS. Now, I'm faced with the ...

It appears that Javascript variables are behaving in a static manner

I am currently building a PHP website with a registration page for users. I am implementing the LOOPJ jQuery Autocomplete script from to enable users to select their country easily. In this process, I encountered an issue where the value of another field ...

What could be the underlying reason for the unusual behavior observed in this range polyfill implementation?

I am attempting to transform an HTML5 range input into a set of radio buttons. Each radio button corresponds to a value within the original range, with text displaying its value. However, I am encountering an issue where the last piece of text, meant to sh ...

The JQxTreeGrid is displaying properly, however, it is not loading the data from the JSON source

Looking for some assistance with loading Json data into a Jquery grid using jqxTreegrid. Currently, the grid is displaying but not the data. Despite no errors in the debugger, I'm unable to figure out the issue. Any help resolving this matter would be ...