I am trying to pass an image and a title field value to PHP, I usually process file uploads straight with PHP using the $_FILES array, I am not sure how to create/pass this array using ajax to PHP. My form:
<form role="form" name="updateProductForm" id="updateProductForm" method="POST" enctype="multipart/form-data">
<input name="imgOne" id="imgOne" type="file" title="Add Image">
<a class="btn btn-primary" name="updateProduct" id="updateProduct">Update Product</a></div>
</form>
And I am trying to use this to pass to PHP:
$('#updateProduct').on('click', function() {
try {
ajaxRequest = new XMLHttpRequest();
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.open("POST", "../Controller/ProductController.php", true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxRequest.send("title=" + $("#title").val() + "&imgOne=" + $("#imgOne"));
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
console.log(ajaxRequest);
}
}
});
In PHP I am trying this:
if (isset($_POST["edit"])) {
$id = $_POST["edit"];
$imgName = $_FILES["file"]["imgName"];
}
Youssef