Hope you can help me with my problem.
The problem is that i put my data
in FormData
but when i'm calling them in php file using echo
there is no values and data existing and gives me error
an
undefined variable
But when im using var_dump()
or print_r()
it show all the values
. and also if i var_dump
the files for the images it throws me also an error
.
Here in html
:
<form id="form" action="myurl.php" method="post" enctype="multipart/form-data" onsubmit="return false">
<input type="file" name="image" accept="image/*"/>
<input type="text" name="description"/>
<input type="submit" />
</form>
Here is my code in ajax
:
function getName(key)
{
key = document.getElementsByName(key)[0];
return key;
}
function getId(key)
{
key = document.getElementById(key);
return key;
}
var url = getId('form').getAttribute('action');
var datas = new FormData();
var xhr = new XMLHttpRequest();
var image = getName('image').value.trim();
var description = getName('description').value.trim();
datas.append('file_image', image.files[0]);
datas.append('description', description);
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(datas);
xhr.onreadystatechange = function()
{
var OK = 4;
var DONE = 200;
if(xhr.readyState == 4 && xhr.status == DONE)
{
alert(xhr.responseText);
}
}
now in my myurl.php
php file
var_dump($_POST);
var_dump($_FILES);
We don't actually using third party script
like jQuery
. We practice native javascript
language.
Thank you.