I'm trying to create a picture upload system where you just press the icon >
Windows Explorer opens up > Select a picture > After selecting, the upload process starts
immediately and automatically. However, every time I'm trying to run that code via AJAX, I'm getting undefined index notice
which can be viewed with "Developer Tools" tab. There's nothing wrong with my PHP, so I'm not posting it in here since the upload process is working just fine without AJAX.
I'm thinking that there might be something wrong with my AJAX code. Any help would be much appreciated! :)
Here's my markup [HTML]
<div class="event-head" id="hover-card">
<i class="fa fa-camera" id="upload-event-cover" title="Update your cover"></i>
<?php $attributes = ['id' => 'update-cover'];?>
<?php echo form_open_multipart('events/upload_cover_photo', $attributes);?>
<input type='hidden' name='event_id' value='<?=$event['event_id']?>'/>
<input type="file" id="event-cover-file" name="userfile" />
<?php echo form_close();?>
</div>
Here's the AJAX part [JS]
<script type="text/javascript">
// Animate the camera icon everytime user hover's over #hover-card
$("#hover-card").on({
mouseenter: function() {
$("#upload-event-cover").animate({
opacity: 1
}, {
duration: 100,
});
},
mouseleave: function() {
$("#upload-event-cover").animate({
opacity: 0
}, {
duration: 100,
});
}
});
// Open "Browse"
$('#upload-event-cover').click(function(e) {
e.preventDefault();
$('#event-cover-file').click();
});
$("#event-cover-file").change(function(e) {
if($(this).val() != "") {
// Send it via AJAX
$("form#update-cover").submit(function(e) {
var from = $(this);
$.ajax({
url: from.attr('action'),
type: from.attr('method'),
data: { userfile: $('#event-cover-file').val() },
beforeSend: function() {
from.find(".comment_submit").hide();
from.find("#spinner_comment").show();
},
success: function(data) {
if(data.st == 1) {
$("#event-cover-file").html(data);
}
}, complete: function() {
from.find("#spinner_comment").hide();
from.find(".comment_submit").show();
}
});
e.preventDefault();
});
}
});
</script>