What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

How to upload a file using xmlhttprequest???

ani707

New member
Joined
Dec 9, 2021
Messages
6
Reaction score
0
HTML Coins
0
Bellow is my code:
How do I pass the data to php file?


Code:
<!DOCTYPE html>
<html>
<head>
<title>SomeTitle</title>

<script>

var object_1 =
{
   id: "test_1",
   name: "test_1",
   title: "Object 1",
   edit: function()
   {
      return "<div id=\"obj1\">" +
                "<label for=\"object1\">" + this.title + "</label>" +
                "<input type=\"text\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";   
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>" + this.title + "</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";   
   },
};

var object_2 =
{
   id: "test_2",
   name: "test_2",
   title: "Object 2",
   data:  function(theData){ return theData; },
   edit: function()
   {
      return "<div id=\"obj2\">" +
                "<label for=\"object2\">" + this.title + "</label>" +
                "<input type=\"text\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";     
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>" + this.title + "</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";   
   },
};

var object_3 =
{
   id: "test_3",
   name: "test_3",
   title: "Upload File",
   data: function(theData){return theData; },
   edit: function()
   {
      return "<div id=\"obj3\">" +
                "<label for=\"object3\">" + this.title + "</label>" +
                "<input type=\"file\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";   
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>Uploaded File:</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";   
   },
};


var object_4 =
{
   edit: function()
   {
      return "<div id=\"obj4\">" +
                "<input type=\"button\" onclick=\"submitForm()\" value=\"uploading Form\">" +
             "</div>";   
   },

   display: function(){    return ""; },  //nothing to show
};

var objArray = ["object_1","object_2","object_3","object_4"];

function editForm()
{
   let objEdit = "";

   for(let i=0, iLen=objArray.length; i<iLen; i++)
   { objEdit = objEdit + eval(objArray[i] + ".edit()"); }
 
   document.getElementById("myData").innerHTML = objEdit;
}

function displayForm()
{
   let objDisplay = "";
   let someData = fromSomewhereArr();

   for(let i=0, iLen=objArray.length; i<iLen; i++)
   { objDisplay = objDisplay + eval(objArray[i] + ".display('" + someData[i] + "')"); }
 
   document.getElementById("myData").innerHTML = objDisplay;
}

function submitForm()
{
   let data = [];
   function sendFileToStoreInFileSystem(data2)
   {  //data2 does not show up ;(
                        function sendToServer(file, data3)
                        {
                            var xhttp = new XMLHttpRequest();
                            xhttp.onreadystatechange =  function()
                            {
                                                        if (this.readyState == 4 && this.status == 200)
                                                        {   alert("File stored"); }
                            };
                                
                            xhttp.open("POST", file, true);
                            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                            xhttp.send(data3);   
                        }


                        sendToServer("/file.php", "filename=" + data2);
   }


   for(let i=0, iLen=objArray.length; i<iLen; i++)
   {
      let dataID = eval(objArray[i] + ".id");
      let dataValue = document.getElementById(" + dataID + ");

      if(dataValue){ data.push(dataValue.value); }
   }
  
   //let processed = processData(data);
   //saveDataIntoIndexedDB(processed);
   //sendDataToStoreInServer(processed);
   sendFileToStoreInFileSystem(data[2]);    //??? store data collected by object_3 (file: picture or pdf)
                                            //How to send data(object_3 (file: picture or pdf)) to a php file, right now my file.php is not getting anything
}

</script>
</head>
<body>

   <h1>My Application</h1><br>
   <div id="myData"></div><br><br>

   <input type="button" onclick="editForm()" value="Edit Form">
   <input type="button" onclick="displayForm()" value="Display Form">

</body>
</html>


Right now I cannot see anything “undefined”
 
Bellow is my code:
How do I pass the data to php file?


Code:
<!DOCTYPE html>
<html>
<head>
<title>SomeTitle</title>

<script>

var object_1 =
{
   id: "test_1",
   name: "test_1",
   title: "Object 1",
   edit: function()
   {
      return "<div id=\"obj1\">" +
                "<label for=\"object1\">" + this.title + "</label>" +
                "<input type=\"text\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";  
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>" + this.title + "</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";  
   },
};

var object_2 =
{
   id: "test_2",
   name: "test_2",
   title: "Object 2",
   data:  function(theData){ return theData; },
   edit: function()
   {
      return "<div id=\"obj2\">" +
                "<label for=\"object2\">" + this.title + "</label>" +
                "<input type=\"text\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";    
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>" + this.title + "</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";  
   },
};

var object_3 =
{
   id: "test_3",
   name: "test_3",
   title: "Upload File",
   data: function(theData){return theData; },
   edit: function()
   {
      return "<div id=\"obj3\">" +
                "<label for=\"object3\">" + this.title + "</label>" +
                "<input type=\"file\" id=\"" + this.id + "\" name=\"" + this.name + "\">" +
             "</div>";  
   },

   display: function(Data)
   {
      return "<div id=\"obj2\">" +
                "<span>Uploaded File:</span>" +
                "<span>" + this.data(Data) + "</span>" +
             "</div>";  
   },
};


var object_4 =
{
   edit: function()
   {
      return "<div id=\"obj4\">" +
                "<input type=\"button\" onclick=\"submitForm()\" value=\"uploading Form\">" +
             "</div>";  
   },

   display: function(){    return ""; },  //nothing to show
};

var objArray = ["object_1","object_2","object_3","object_4"];

function editForm()
{
   let objEdit = "";

   for(let i=0, iLen=objArray.length; i<iLen; i++)
   { objEdit = objEdit + eval(objArray[i] + ".edit()"); }
 
   document.getElementById("myData").innerHTML = objEdit;
}

function displayForm()
{
   let objDisplay = "";
   let someData = fromSomewhereArr();

   for(let i=0, iLen=objArray.length; i<iLen; i++)
   { objDisplay = objDisplay + eval(objArray[i] + ".display('" + someData[i] + "')"); }
 
   document.getElementById("myData").innerHTML = objDisplay;
}

function submitForm()
{
   let data = [];
   function sendFileToStoreInFileSystem(data2)
   {  //data2 does not show up ;(
                        function sendToServer(file, data3)
                        {
                            var xhttp = new XMLHttpRequest();
                            xhttp.onreadystatechange =  function()
                            {
                                                        if (this.readyState == 4 && this.status == 200)
                                                        {   alert("File stored"); }
                            };
                               
                            xhttp.open("POST", file, true);
                            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                            xhttp.send(data3);  
                        }


                        sendToServer("/file.php", "filename=" + data2);
   }


   for(let i=0, iLen=objArray.length; i<iLen; i++)
   {
      let dataID = eval(objArray[i] + ".id");
      let dataValue = document.getElementById(" + dataID + ");

      if(dataValue){ data.push(dataValue.value); }
   }
 
   //let processed = processData(data);
   //saveDataIntoIndexedDB(processed);
   //sendDataToStoreInServer(processed);
   sendFileToStoreInFileSystem(data[2]);    //??? store data collected by object_3 (file: picture or pdf)
                                            //How to send data(object_3 (file: picture or pdf)) to a php file, right now my file.php is not getting anything
}

</script>
</head>
<body>

   <h1>My Application</h1><br>
   <div id="myData"></div><br><br>

   <input type="button" onclick="editForm()" value="Edit Form">
   <input type="button" onclick="displayForm()" value="Display Form">

</body>
</html>


Right now I cannot see anything “undefined”
 
This may seem to work:

function submitForm()
{
let data = [];
function sendFileToStoreInFileSystem(data2)
{ //data2 does not show up ;(
function sendToServer(file, data3)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{ alert("File stored"); }
};

xhttp.open("POST", file, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(data3);
}


sendToServer("/file.php", "filename=" + data2);
}



//let processed = processData(data);
//saveDataIntoIndexedDB(processed);
//sendDataToStoreInServer(processed);
//sendFileToStoreInFileSystem(data[2]);

const fileToUpload = document.getElementById("test_3").files;
sendFileToStoreInFileSystem(fileToUpload);


}
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back