I'm trying to implement the profile picture change, saving the new one on my postgres db, to do it I pass the data to php with ajax, but if I put the ajax call into the change function it gives me these errors:
jquery-1.11.0.min.js:4 Uncaught TypeError: Illegal invocation
And:
Uncaught (in promise) TypeError: Failed to execute 'arrayBuffer' on 'Blob': Illegal invocation
The logic I'm using is: Set the new preview profile image, and then call ajax that pass the new image to php to insert it also into the db to store it. This is my script:
/* change profile picture */
$(document).ready(function() {
$("#profilepic__image").click(function(e) {
$("#imageUpload").click();
});
$("#profilepic__content").click(function(e) {
$("#imageUpload").click();
});
function fasterPreview( uploader ) {
if ( uploader.files && uploader.files[0] ){
$('#profilepic__image').attr('src', window.URL.createObjectURL(uploader.files[0]) );
}
}
$('#imageUpload').on('click', function (){
fasterPreview( this );
$('#imageUpload').trigger('change');
ajaxCall( this );
});
function ajaxCall(uploader) {
if ( uploader.files && uploader.files[0] ) {
$(document).ready(function(){
$.ajax({
url: "saveNewProfileImg.php",
method: "post",
data: { "input": uploader.files[0], "key":email},
success: function(result){
console.log('Image update succ');
},
error: function() {
console.log('Image update err');
}
});
});
}
}
});
If I remove the ajax call it works fine but obv it doesn't call php so it doesn't works the db insert. Searching the possible fix I tried to do use .on function instead of change, or also .live, but both cases not worked. I don't know if this is the correct way to do it, I'm learning.
If html is needed:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title></title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="style.css" rel="stylesheet"/>
<link href="all.css" rel="stylesheet"/>
<link href="all.min.css" rel="stylesheet"/>
</head>
<body class="act-home">
<div class="main-content">
<div class="home-container">
<div class="profile-container">
<div class="banner-profile-container">
<div class="profilepic-name-container">
<img class="profilepic__image" id="profilepic__image" src="../media/profilepic.jpg" style="border-radius: 55px;" width="150px" height="150px" alt="">
<div class="profilepic__content" id="profilepic__content">
<img src="../media/cameraiconprofile.png" class="profilepic__icon" id="profileImage"></span>
<span class="profilepic__text">Edit Image</span>
</div>
<input id="imageUpload" type="file" name="profile_photo" placeholder="Photo" required="" capture>
<span class="banner-username">Shibaka</span>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
</body>