Note that there are some explanatory texts on larger screens.

plurals
  1. POfile uploading in ajax page
    primarykey
    data
    text
    <p>I am trying to do a file uploading on an ajax result page.</p> <p>the main.php file is a basic ajax code as below:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; 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 &amp;&amp; xmlhttp.status == 200) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "file.php", true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="myDiv"&gt;&lt;h2&gt;Let AJAX change this text&lt;/h2&gt;&lt;/div&gt; &lt;button type="button" onClick="loadXMLDoc()"&gt;Change Content&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and the file uploading form file.php is as follow:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" name="form1" enctype="multipart/form-data" method="post" action="fileupload.php"&gt; &lt;p&gt; &lt;label for="file"&gt;&lt;/label&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" name="submit" id="submit" value="Submit" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I have a 3rd file which process the file uploading fileupload.php</p> <pre><code>&lt;?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 "&lt;BR&gt;"; if ($_FILES["file"]["error"] &gt; 0) { echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;"; } else { echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;"; echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;"; if (file_exists($path)) { echo $path . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], $path); echo "Stored in: " . $path; } } ?&gt; </code></pre> <p>The page is working fine when I point the form post to fileupload.php</p> <p>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:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;script type="text/javascript"&gt; 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 &amp;&amp; xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","file.php",true); xmlhttp.send(); } &lt;/script&gt; &lt;?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 "&lt;BR&gt;"; if ($_FILES["file"]["error"] &gt; 0) { echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;"; } else { echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;"; echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;"; if (file_exists($path)) { echo $path . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], $path); echo "Stored in: " . $path; } } ?&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" name="form1" enctype="multipart/form-data" method="post" action="fileupload.php"&gt; &lt;p&gt; &lt;label for="file"&gt;&lt;/label&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;/p&gt; &lt;p&gt; &lt;button type="button" onClick="fileup()"&gt;Upload&lt;/button&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>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.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload