Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:38:46+00:00 2026-06-07T04:38:46+00:00

I needed a quick explanation on how to get an AJAX style file upload

  • 0

I needed a quick explanation on how to get an AJAX style file upload going using a hidden iframe. Here’s the portion of the HTML code pertaining to the form:

<div id = "file" class = "info">        
    <form id="file_upload_form" method="post" enctype="multipart/form-data" action='script/uploadScript'>
        <input name="file" id="file" size="27" type="file" /><br />
        <input id = "uploadSubmit" type="submit" name="action" value="Upload" />
        <iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
    </form>
</div>

Here’s the relevant PHP:

public function uploadScript(){

    $returnVals = array();

    if ($_FILES["file"]["error"] > 0){
        $returnVals['error'] =  "Error: " . $_FILES["file"]["error"] . "<br />";
    }else{
        if ($_FILES["file"]["type"] != "text/plain"){
            $returnVals['error'] = "Badtype";
        }else{
            $returnVals['text'] =  file_get_contents($_FILES["file"]["tmp_name"]);
        }
    }

    echo json_encode($returnVals);
}

So, essentially, the PHP gets the file and checks if it’s a text file. Then, I want to access the returned json within javascript. That’s where I get confused…

$("#file_upload_form").submit(function() {
    $("#file_upload_form").target = "upload_target";
    $("#upload_target").onload = uploadDone();

});

Ideally, when the upload is complete and the iframe is loaded, this should call

function uploadDone() {
    alert($("#upload_target").contents().find("body").text());  
}

But, that’s always blank. Essentially, the returnVals are placed into the iframe body, but after the alert, so the alert has nothing in it. So, is there a way to remedy this little flaw?

Is there any better explanation on how to do this? I’ve been banging my head against it with different sample codes and etc all day with no luck :/

I essentially want to be able to alert the text returned via the php code. Btw, I’m doing it this way because I want it to be able to work on IE7 and all browsers after.

I’d appreciate any help a ton. Thanks for the time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:38:47+00:00Added an answer on June 7, 2026 at 4:38 am

    Well, I’ve used something like that:

    $("#upload_target")[0].contentWindow.document.body.innerHTML
    

    … instead. But the general approach is similar: check this string, and if it’s empty, just do nothing (i.e., return immediately). Otherwise proceed with analyzing this json.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built a quick program that needed to loop through an enormous log file
A quick question here: is it faster to load a large XML file (just
Have a quick question. I am using a SQLite database file in my C#
Needed to write a server text file as the output of a business process
i needed help. How do i get domain from a string? For example: Hi
What is needed to run .rb file in a web browser? (My first day
I needed to push a quick fix for a bug, so I stashed the
I already looked for my answer but I didn't get any quick response for
I needed to put in a flyout menu real quick for a client. I
I'm using a built-in benchmarking module for some quick and dirty tests. It gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.