Skip to content

Instantly share code, notes, and snippets.

@malikkurosaki
Created February 6, 2018 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malikkurosaki/8cbb6725a88d7aebc74ff70d485f1544 to your computer and use it in GitHub Desktop.
Save malikkurosaki/8cbb6725a88d7aebc74ff70d485f1544 to your computer and use it in GitHub Desktop.
this is super basic ajax upload image
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="../core/w3.css">
<style >
#textareaB{
}
img{
width: 100%;
}
</style>
</head>
<body>
<form id="form" method="post" action="upload.php" enctype="multipart/form-data" >
<input id="pilih" type="file" name="up" >
<input id="submit" type="submit" value="upload">
</form>
</body>
<script type="text/javascript" src="malik-adm.js?i+=1"></script>
<script>
var form = document.getElementById("form");
var pilih = document.getElementById("pilih");
form.addEventListener("submit",function(event){
event.preventDefault();
var gambar = pilih.files[0];
var formdata = new FormData(form);
var xupload = new XMLHttpRequest();
xupload.open("POST","upload.php",true);
xupload.onload = function(){
if(xupload.status == 200){
alert(xupload.responseText);
}else{
alert("gagal");
}
};
xupload.send(formdata);
});
fileimage.addEventListener("change",function(){
showimage.src = window.URL.createObjectURL(this.files[0]);
});
</script>
</html>
//upload.php
<?php
$dir = "../lib/";
$file = $dir.basename($_FILES['up']['name']);
define("SUKSES","sukses");
define("GAGAL","gagal");
if(move_uploaded_file($_FILES['up']['tmp_name'],$file)){
echo SUKSES;
}else{
echo GAGAL;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment