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

  • SEARCH
  • Home
  • 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 6601559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:46:10+00:00 2026-05-25T18:46:10+00:00

Simply this the code for PHP page the uses AJAX to upload file, but

  • 0

Simply this the code for PHP page the uses AJAX to upload file, but it doesn’t work and I don’t know why.

edit

When I click submit it reload the entire page and jQuery doesn’t retrieve the data posted
by PHP to the iframe

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Access the $_FILES global variable for this specific file being uploaded
    // and create local PHP variables from the $_FILES array of information
    $fileName = $_FILES["uploaded_file"]["name"]; // The file name
    $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder

    // Place it into your "uploads" folder mow using the move_uploaded_file() function
    $moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");

    // Check to make sure the move result is true before continuing
    if($moveResult == true) {
        echo "<div id='filename'>$fileName</div>";
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Ajax File Upload</title>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                // If submit button is clicked
                $('form#upload').submit(function() {

                    $('#upload_wrapper').hide();
                    $('#loading').show();

                    // Get the uploaded file name from the iframe
                    $('#upload_target').unbind().load( function() {
                        var img = $('#upload_target').contents().find('#filename').html();

                        $('#loading').hide();

                        // Load to preview image
                        if(img) {
                            $('#preview').show();
                            $('#preview').attr('src', 'uploads/'+img);
                            $('#image_wrapper').show();
                        }
                    });
                });
            });

            function drawUploader() {
                $("#aa").append("<div id='upload_wrapper'>"+
                                "<form id='upload' name='upload' enctype='multipart/form-data' method='post' action='index.php' target='upload_target'>"+
                                "<input name='uploaded_file' type='file' size='30' id='uploaded_file'  />"+
                                "<input id='sent' name='sent' type='submit'  value='Upload' />"+
                                "</form>"+
                                "</div>"+
                                "<div id='loading' style='background:url(ajax-loader.gif) no-repeat left; height:50px; width:370px; display:none;'>"+
                                "<p style='margin-left:40px; padding-top:15px;'>Uploading File... Please wait</p>"+
                                "</div>"+
                                "<div id='image_wrapper' style='display:none;'><img id='preview' src='' /></div>");
                $("#aa").clone();
            }       
        </script>
    </head>

    <body>
        <input type="button" onclick="drawUploader()" value="start uplaod" />
        <div id="aa"></div>
        <iframe id="upload_target" name="upload_target" src="" style="width: 10px; height: 10px; display: none"></iframe>
    </body>
</html>
  • 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-05-25T18:46:11+00:00Added an answer on May 25, 2026 at 6:46 pm

    It doesn’t look like you are calling preventDefault() from the submit handler, so the form’s default behaviour will be triggered, i.e. submit the form and render the response.

    $('form#upload').submit(function(event) {
        event.preventDefault();
        ...
    });
    

    EDIT 1

    Added to delay registering the submit handler until after the form is actually on the page / in the DOM.

        <script type="text/javascript">
            function drawUploader() {
                function mySubmitHandler (event) {
                    // maybe not required - event.preventDefault();
    
                    $('#upload_wrapper').hide();
                    $('#loading').show();
    
                    // Get the uploaded file name from the iframe
                    $('#upload_target').unbind().load( function() {
                        var img = $('#upload_target').contents().find('#filename').html();
    
                        $('#loading').hide();
    
                        // Load to preview image
                        if(img) {
                            $('#preview').show();
                            $('#preview').attr('src', 'uploads/'+img);
                            $('#image_wrapper').show();
                        }
                    });
                }
    
                $("#aa").append("<div id='upload_wrapper'>"+
                                "<form id='upload' name='upload' enctype='multipart/form-data' method='post' action='index.php' target='upload_target'>"+
                                "<input name='uploaded_file' type='file' size='30' id='uploaded_file'  />"+
                                "<input id='sent' name='sent' type='submit'  value='Upload' />"+
                                "</form>"+
                                "</div>"+
                                "<div id='loading' style='background:url(ajax-loader.gif) no-repeat left; height:50px; width:370px; display:none;'>"+
                                "<p style='margin-left:40px; padding-top:15px;'>Uploading File... Please wait</p>"+
                                "</div>"+
                                "<div id='image_wrapper' style='display:none;'><img id='preview' src='' /></div>");
    
                // If submit button is clicked
                $('form#upload').submit(mySubmitHandler);
    
                $("#aa").clone();
            }       
        </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code reloads the entire page rather than simply replacing the #form element every
Ho to make this simple xml with php using DOM? Full code will be
any simply way ? this is my code: var a=[1,2,3,4] a.slice(0,1) alert( a) and
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I have a data that looks like this . And my code below simply
I have a simple page view tracker that uses a combination of PHP and
My objective with this code is when an user click in edit button, is
Drupal version 6.12 I have a page whose input format is PHP. I simply
Hi, I know this should be really simple but I am just too new
I have a simple PHP page (for testing) that simply calls header(Location: http://www.example.com );exit;

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.