I have a problem with PHP uploading images and javascrit progress bar I get a problem when I add PHP dynamic "select option" field. I need this option to alow user to chose wich of his advert want to add image. This is my form:
<div id="uploaded">
</div>
<div>
<form action="" method="post" enctype="multipart/form-data">
<input name="image" id="image_id" type="file" size="25" value="Odaberi sliku" />
<p>
<select name="page_id" class="option_field">
<?php
foreach ($pages as $page) {
echo '<option value="' . $page['page_id'] . '">' . $page['name_ad'] . '</option>';
}
?>
</select>
</p>
<input type="submit" id="submit" value="Add Images" class="submit_btn"/>
</form>
</div>
<div class="upload_progress" id="upload_progress"></div>
With this option field I'm getting data with $_POST['page_id'] . When I disable javascript in my browser uploading images work fine but with javascript I cant get value of "page_id". This is my javascript code:
var handleUpload = function(event) {
event.preventDefault();
event.stopPropagation();
var fileInput = document.getElementById('image_id');
var data = new FormData();
data.append('javascript', true);
if(fileInput.files[0].size > 1050000) {
document.getElementById("image_id").innerHTML = "Image too big (max 1Mb)";
alert('Fotografija koju želite dodati je veća od 1Mb!');
window.location="upload_images.php"
return false;
}
for (var i =0; i < fileInput.files.length; ++i) {
data.append('image', fileInput.files[i]);
}
var request = new XMLHttpRequest();
request.upload.addEventListener('progress', function(event) {
if (event.lengthComputable) {
var percent = event.loaded / event.total;
var progress = document.getElementById('upload_progress');
while (progress.hasChildNodes()) {
progress.removeChild(progress.firstChild);
}
progress.appendChild(document.createTextNode(Math.round(percent * 100) + ' %'));
}
});
request.upload.addEventListener('load', function(event) {
document.getElementById('upload_progress').style.display = 'none';
});
request.upload.addEventListener('error', function(event) {
alert('Dodavanje slika nije bilo uspješno! Pokušajte ponovo.');
});
request.addEventListener('readystatechange', function(event) {
if (this.readyState == 4) {
if(this.status == 200) {
var links = document.getElementById('uploaded');
window.location="upload_images.php?success"
console.log(this.response);
} else {
console.log('Server replied with HTTP status ' + this.status);
}
}
});
request.open('POST', 'upload_images.php');
request.setRequestHeader('Cache-Control', 'no-cache');
document.getElementById('upload_progress').style.display = 'block';
request.send(data);
}
window.addEventListener('load', function(event) {
var submit = document.getElementById('submit');
submit.addEventListener('click', handleUpload);
});
Everything works fine before I add select option field and everything works fine without javascript. Problem is that I can't get value with $_POST['page_id'] when javascript works.
request.open('POST', 'upload_images.php', true);
and if you are IE you must changeaddEventListener
toattachEvent
– Alex Ruhl Apr 21 at 12:34