Hello I have been working on a site where you can submit images. I dont want the page to be reloaded when the picture is submitted. After the picture is posted to the php page it is stored a a BLOB in a MYSQL database.
form id="form2" action="saveImage.php" method="post" enctype="multipart/form-data">
<input id="thumbage2" name="Image1" class="ModalArea2" type="file" style="display:none">
<center><label for="thumbage2" id="labelthumb2" style="margin-top:35px;">Choose Image</label></center>
<button class="ModalButton2" >Submit</button>
<a class="ModalButton2" onclick="cancel2();">Cancel</a>
</form>
The above is my HTML code. Here is my javascript/jQuery code:
$(':file').change(function(){
var file = this.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
//Your validation
//console.log(name);
});
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'upload.php', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: beforeSendHandler,
success: completeHandler,
error: errorHandler,
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
cancel2();
});
I copied this code from a website but they didn't do a great job of explaining how it works. So at the moment I have the data being posted to my PHP file and it works. The problem is that the PHP file is loaded. I thought the whole point of JQuery was not to load the file and to stay on the same page. Why doesn't this happen? Why do I keep getting the PHP page loaded in my browser? Thanks if you solve this for me I would be over the Moon It's been a real kick in the adams apple for me lately.