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 6880279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:57:14+00:00 2026-05-27T04:57:14+00:00

I am trying to do a file uploading on an ajax result page. the

  • 0

I am trying to do a file uploading on an ajax result page.

the main.php file is a basic ajax code as below:

<html>
 <head>
<script type="text/javascript">
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "file.php", true);
        xmlhttp.send();
    }
</script>    
 </head>
 <body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onClick="loadXMLDoc()">Change Content</button>

</body>
</html>

and the file uploading form file.php is as follow:

<!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>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="fileupload.php">
  <p>
    <label for="file"></label>
    <input type="file" name="file" id="file" />
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>
</body>
</html>

I have a 3rd file which process the file uploading fileupload.php

<?php
$file_fullname= $_FILES['file']['name'];
$file_ext1 = pathinfo($file_fullname, PATHINFO_EXTENSION);
$file_ext = ".".$file_ext1;
$file_name = pathinfo($file_fullname,PATHINFO_BASENAME);
$temp_name = str_replace($file_ext,'',$file_fullname);
$new_file_name = md5($temp_name . $dt);
echo $path= "upload/".$new_file_name . $file_ext;
$photo = $new_file_name . "" . $file_ext;
echo "<BR>";

   if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists($path))
      {
      echo $path . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"], $path);
      echo "Stored in: " . $path;
      }
    }
?>

The page is working fine when I point the form post to fileupload.php

But when I moved the contents of fileupload.php into the file.php with the ajax code. it seems like the file uploading process is not run. Updated code file2.php is as follow:

<!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>Untitled Document</title>
    <script type="text/javascript">
    function fileup()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","file.php",true);
    xmlhttp.send();
    }
    </script>

<?php
$file_fullname= $_FILES['file']['name'];
$file_ext1 = pathinfo($file_fullname, PATHINFO_EXTENSION);
$file_ext = ".".$file_ext1;
$file_name = pathinfo($file_fullname,PATHINFO_BASENAME);
$temp_name = str_replace($file_ext,'',$file_fullname);
$new_file_name = md5($temp_name . $dt);
echo $path= "upload/".$new_file_name . $file_ext;
$photo = $new_file_name . "" . $file_ext;
echo "<BR>";

   if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists($path))
      {
      echo $path . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"], $path);
      echo "Stored in: " . $path;
      }
    }
?>
</head>

<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="fileupload.php">
  <p>
    <label for="file"></label>
    <input type="file" name="file" id="file" />
  </p>
  <p>
    <button type="button" onClick="fileup()">Upload</button>
  </p>
</form>
</body>
</html>

Can advice?? I am just trying to have the file uploading done in the ajax result section so there is no need to refresh the entire page.

Thanks!

  • 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-27T04:57:15+00:00Added an answer on May 27, 2026 at 4:57 am

    Historically, the values of <input type="file"> elements have been inaccessible to JavaScript as a security precaution.

    The File API has been defined to offer some support for interacting with files in JavaScript, and it has some support — http://caniuse.com/fileapi.

    However, if compatibility with older browsers is a concern, you won’t be able to use Ajax to upload files.

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

Sidebar

Related Questions

I am trying to code for unzipping a file after uploading it on the
I am trying to retrieve a file name from one page where the php
I'm trying to use Uploadify , an Ajax file uploader, with Play Framework .
Im trying print Excel file data on a page. To do it i used
I am trying out ajax file upload in django .I coded the javascript ,django
I'm trying to create file upload using iframe(ajax effect) but I need to know
Hey I am trying to make a notification for my file uploading notification. This
I'm trying to use ajax file upload plugin( file uploader plugin ) to upload
I'm using plupload to make an ajax file uploading. Now the plupload.Uploader class has
I'm trying to make a file field for uploading images. However, mine has a

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.