I'm trying to upload a photo and pass it's data and one more data on another php file.When I select the photo and upload it a current php file give an error: Uncaught ReferenceError: PZ71AUGW5O295 is not defined. PZ71AUGW5O295 is the teamCode which gets.Sorry for the possible grammatical mistakes.
<?php
$teamCode = $_GET['tc'];
?>
var uploader = {
url: "faces.php",
method: "POST",
success: function (data){
}
};
function update() {
searchParams = new FormData();
searchParams.append('teamCode', <?php echo $teamCode; ?>); // the error will be here
searchParams.append('selfie', document.getElementById("faceinput").files[0]);
uploader.cache = false;
uploader.contentType = false;
uploader.processData = false;
uploader.xhr = function () {
var jqXHR = null;
if (window.ActiveXObject) {
jqXHR = new window.ActiveXObject("Microsoft.XMLHTTP");
} else {
jqXHR = new window.XMLHttpRequest();
}
//Upload progress
jqXHR.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round((evt.loaded * 100) / evt.total);
//Do something with upload progress
$(".progress-bar").css("width", percentComplete + "%");
}
}, false);
return jqXHR;
}
uploader.data = searchParams;
$.ajax(uploader);
}
After I upload the photo I get the error:
Uncaught ReferenceError: PZ71AUGW5O295 is not defined
at update (upload_photo.php?tc=PZ71AUGW5O295:86)
at HTMLInputElement.<anonymous> (upload_photo.php?tc=PZ71AUGW5O295:71)
at HTMLInputElement.dispatch (jquery.min.js:3)
at HTMLInputElement.r.handle (jquery.min.js:3)
You are just echoing your php variable in js, then that parsed data would be transferred to js code, so now look what your js code would look like.
searchParams.append('teamCode', PZ71AUGW5O295);
In this case js would try to look whether PZ71AUGW5O295 variable exists, of course its not.
Other then that it look like your are doing a lot of things in a not good way. f.e. passing image with GET not POST, getting submitted with php image to create another request. Whats the idea? Just build your request in image upload page with js and then pass it, whats the idea of having php code to then again send another request.