i have problem pass data from selected file (image)
i will move the image to localhost directory
here my simple code
function uploadFile(){
event.preventDefault();
var input = document.getElementById("uploadImage");
file = input.files[0];
if(file != undefined){
formData= new FormData();
if(!!file.type.match(/image.*/)){
formData.append("image", file);
$.ajax({
url: "service.php?aksi=A",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data){
alert('data);
}
});
}else{
alert('Not a valid image!');
}
}else{
alert('Input something!');
}
}
here my simple html code
<input type="file" required="" id="uploadImage" name="gambar" >
<button id="gantiphoto" name="submit" onclick="uploadFile()" class="btn btn-default">Upload </button>
and call it to specific php function
if ($_POST["aksi"]=="A"){
$dir = "asset/";
move_uploaded_file($_FILES["image"]["tmp_name"], $dir. $_FILES["image"]["name"]);
}
elseif ($_POST["aksi"]=="B"){
}
i try
$.ajax({
url: "service.php?aksi=A",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(data){
alert('data);
}
});
and
$.ajax({
url: "service.php",
type: "POST",
data: "aksi=A"+formData,
processData: false,
contentType: false,
success: function(data){
alert('data);
}
});
but the response still
Notice: Undefined index: image in C:xampphtdocsrutelaservice.php on line 166
or
Notice: Undefined index: aksi in C:xampphtdocsrutelaservice.php on line 7
i still dont understand how to pass this image file,. im success with text but cant solve this image pass data
im appreciate ur answer, im just newbie in ajax
2
Answers
You Can Add One More formData Filed
Also Change Url to This
There are a couple things I might change to make this work:
In your PHP you should first try a test to make sure but I would switch to
$_GET
here not$_POST
: