How to fetch form data in JavaScript using AJAX and PHP || Insert data in PHP through JavaScript
on
Get link
Facebook
X
Pinterest
Email
Other Apps
Using FormData Objects
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest.
It is primarily intended for use in sending form data, but can be used
independently from forms in order to transmit keyed data. The
transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to multipart/form-data.
You can build a FormData object yourself, instantiating it then appending fields to it by calling its append() method, like this:
var formData =newFormData();
formData.append("username","Groucho");
formData.append("accountnum",123456);// number 123456 is immediately converted to a string "123456"// HTML file input, chosen by user
formData.append("userfile", fileInputElement.files[0]);// JavaScript file-like objectvar content ='hey!';// the body of the new file...var blob =newBlob([content],{ type:"text/xml"});
formData.append("webmasterfile", blob);var request =newXMLHttpRequest();
request.open("POST","http://foo.com/submitform.php");
request.send(formData);
Note: The fields "userfile" and
"webmasterfile" both contain a file. The number assigned to the field
"accountnum" is immediately converted into a string by the FormData.append() method (the field's value can be a Blob, File, or a string: if the value is neither a Blob nor a File, the value is converted to a string).
This example builds a FormData instance containing values for fields named "username", "accountnum", "userfile" and "webmasterfile", then uses the XMLHttpRequest method send() to send the form's data. The field "webmasterfile" is a Blob. A Blob
object represents a file-like object of immutable, raw data. Blobs
represent data that isn't necessarily in a JavaScript-native format. The
File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system. In order to build a Blob you can invoke the Blob() constructor.
You can also send files using FormData. Simply include an element of type file in your :
<formenctype="multipart/form-data"method="post"name="fileinfo"><label>Your email address:</label><inputtype="email"autocomplete="on"autofocusname="userid"placeholder="email"requiredsize="32"maxlength="64"/><br/><label>Custom file label:</label><inputtype="text"name="filelabel"size="12"maxlength="32"/><br/><label>File to stash:</label><inputtype="file"name="file"required/><inputtype="submit"value="Stash the file!"/></form><div></div>
Then you can send it using code like the following:
var form = document.forms.namedItem("fileinfo");
form.addEventListener('submit',function(ev){var oOutput = document.querySelector("div"),
oData =newFormData(form);
oData.append("CustomField","This is some extra data");var oReq =newXMLHttpRequest();
oReq.open("POST","stash.php",true);
oReq.onload =function(oEvent){if(oReq.status ==200){
oOutput.innerHTML ="Uploaded!";}else{
oOutput.innerHTML ="Error "+ oReq.status +" occurred when trying to upload your file.
";}};
oReq.send(oData);
ev.preventDefault();},
false);
Note: If you pass in a reference to the form the request method specified in the form will be used over the method specified in the open() call.
You can also append a File or Blob directly to the FormData object, like this:
data.append("myfile", myBlob,"filename.txt");
When using the append() method it is possible to use the third optional parameter to pass a filename inside the Content-Disposition header that is sent to the server. When no filename is specified (or the parameter isn't supported), the name "blob" is used.Source: Mozilla Firefox Developer Community
Comments
Post a Comment