Executing a javascript function from an ajax-loaded webpage

I have a form where I upload a file using an ajax call that includes an API request. Once the API call is successful, I need to update a table displaying the list of uploaded files. Initially, I attempted calling a JavaScript function within the ajax page, but encountered an issue where the function was undefined. I resolved this by ensuring that clarifications.js is included before the JavaScript function.

Below is the form (the JavaScript file with the function is included at the top of the page):

<div class='input_table_title'>Upload file:</div>
<div style='float:left;'>
<form name='upload_my_files' action='ajax/clarifications/handle_upload.php' method='post' enctype='multipart/form-data' target='upload_target'>
<input type='file' name='file_upload' />
<input type='hidden' name='notification_id' value='<?php echo $value->NotifiNumber; ?>' />
<input type='submit' value='Upload'>
</form>
<iframe id='upload_target' name='upload_target' src='' style='width:0;height:0;border:0px solid #fff;'></iframe>
</div>

In the handle_upload.php file, I make the API call and at the end include clarifications.js before calling the function:

<script src="(position)/clarifications.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
createTable("<?php echo $outcome ?>", "<?php echo $uploaded_filename ?>");
</script>

The clarifications.js file contains the declared function (which is also included in the form page):

function createTable(result, filename) {

 if (result == 'success'){

  var success = document.getElementById('clarification_success');

  success.style.display = "block";
  success.innerHTML = "File "+filename+" successfully uploaded.";

  // Update the list of uploads
  var list = document.getElementById('table_clarification');
  var myElement = document.createElement("td");
  myElement.innerHTML = filename;

  list.appendChild(myElement);
 }

 return true;

}

The div clarification_success is used to display a success message, while the table_clarification ID is for adding the uploaded filename's row.

The upload is successful, though initially the createTable function was not defined. Edit: Resolved

Another issue I'm facing is that the function cannot find the ID "clarification_success", likely because it's looking within the iframe instead of the main page. Is my assumption correct? If so, how can I address this?

Note: Due to restrictions, I am unable to use jQuery and must rely on pure JavaScript.

Answer №1

Based on my understanding, the iframe content needs to be a standalone page. This page is generated dynamically on the server and then sent back to the client as a response. It's likely that this page should include

<script src="(path)/clarifications.js"></script>
in the header.

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

Creating a JSON File with an Illustrator Script

Currently, I've been working diligently on developing a system that allows me to export my swatches from Illustrator as a JSON object. This will greatly simplify the process of updating my App. By utilizing the illustrator scripting API, I've suc ...

Identify all elements that include the designated text within an SVG element

I want to target all elements that have a specific text within an SVG tag. For example, you can use the following code snippet: [...document.querySelectorAll("*")].filter(e => e.childNodes && [...e.childNodes].find(n => n.nodeValue ...

Issue with extraneous event being activated upon bootstrap collapse button usage

I have implemented a functionality using Bootstrap 5.2 where I have two buttons that can hide content by utilizing the Bootstrap collapse plugin. <div class="col-12 col-sm-auto"> <span class="pe-2"> ...

Clear form fields and remove any validation errors upon closing a div with jQuery

A popup div is housing a form that I want to reset both errors and fields when the form is closed. My plan was to add code in the disablePopup function to reset the form. Some important things to mention are that this is a Django project and I am using Aja ...

What is the best way to showcase a circular dot to indicate active status in a React component

In previous versions of react, the code displayed either "active" or "inactive" for a user. I now want to change it to display a small red or green dot instead. How can I achieve this? ...

Retrieve the JSON array embedded within the JSON string

Can someone help me extract the JSON array from a JSON string? I've been struggling to make it work. Here is the code snippet for reference: // I need assistance with this var all_barcodes = '{"VAM12345":{"colour":"red","size":"32"},"VAM456789" ...

When attempting to render a base64 string in an <img> tag using ReactJS, an error message ERR_INVALID_URL is displayed

I am currently working on displaying server-generated images (specifically matplotlib graphs) in a ReactJS module without needing to save the files on the server. To achieve this, I decided to use base64 to create an image string. When it comes time to sh ...

Clicking a button in JavaScript can pass an ID by triggering an onClick

Here is the function that triggers when the button is clicked <button type="button" class="btn btn-info " onClick= "show('<?php echo $data ?>')" name="show" >Show</button> This is the show function defined in the .js file f ...

Fetching data from multiple tables in CodeIgniter using PHP and jQuery and returning it asynchronously with Ajax

Currently, I am utilizing jQuery to retrieve JSON data through AJAX from a CodeIgniter backend and MySQL database, which is functioning correctly. However, the issue I am facing is that in addition to fetching the data returned to the jQuery function, I al ...

What is the process for updating the ID within a Select2 ajax script function?

I am facing an issue with my select2 ajax script. The unique_ID in my table field is named "no", which is causing me to be unable to select an item in Select2 drop down. However, when I changed the name of my database table field from "no_donatur" to "ID", ...

Automatically selecting checkboxes from an array using ReactJS

Hello there, I am a beginner working with react and I could really use some help with the issue below. Can you assist me? I am trying to figure out how to populate or check/uncheck checkboxes based on an Array's result. Specifically, I want to have ...

Adjusting the height of a textarea with javascript

I am trying to reset the contents and height of an auto resizing textarea element. My attempts so far include: document.getElementById('textarea').value = ''; As well as: document.getElementById('textarea').attribute(&apos ...

Unable to transfer the value of one polymer property to another polymer property, resulting in the value being returned as a function

In the code snippet below, I am utilizing Iron Ajax with Polymer to make an HTTP request. The values for the body and last-response are passed using Polymer properties. In the requestBody property, hardcoded values such as start, end, and name are being re ...

Inability of AngularJS and Google Maps API V3 to Geocode an Address Iteratively

I'm currently attempting to geocode markers for each location in an AngularJS scope accessible through $scope.locations Unfortunately, I keep encountering the error message TypeError: Cannot call method 'geocode' of undefined To address th ...

Issue with extended waiting times in polling

I am currently working on a chatroom using the long poll method. However, I'm facing an issue where every time a long poll occurs and I refresh the page in Chrome or try to send another async request, everything times out. This means I can't load ...

transfer information using dropzone

I am currently working with Dropzone and have implemented the following code: <form action="#" class="dropzone" ></form> Within the JavaScript code, the dropzone function is set to upload images only when a button with the id "startUpload" i ...

Angular: Modifying the parent scope from a child component

Hey there! So I'm a beginner in this whole programming thing, but I'm currently working on a small application where I can add and update items with details using Ionic, Cordova, and AngularJS. However, I've hit a roadblock with the followin ...

Ways to eliminate a specific array from another array using JavaScript

0: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …} 1: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_amount: 9500, toppings: 500, …} 2: {id: 1553825061863, name: "Thai Milk Tea", qty: "1", total_am ...

``The Dynamic Checkbox Selection Process using Ajax, Jquery, and PHP

I'm a bit confused about the next steps... <input id="" type="checkbox" name="" onClick="" /> <td> <h4>PDF Document</h4> <p> Do you have your own menu or flyer? You can add it to your App </p> ...

What is the best way to incorporate an apostrophe into a string so that it can be displayed in a tooltip using jQuery

There is a text or string stored inside the 'data' variable, containing some texts with apostrophes. The issue I'm facing is that the tool tip is not displaying the text after the apostrophe symbol. How can I include all texts, including apo ...