I have php file which returns json object. In that if errors I'll set them like this,
if(move_uploaded_file($_FILES['file']['tmp_name'][$position], $uploaddir.$name)){
$uploaded[] = array(
'name' => $name,
'file' => 'assets/uploads/'.$name
);
}
else
{
$error[] = array('error' => $_FILES['file']['tmp_name'][$position].' file uploading failed');
}
If errors are set I need to display them. Else I want to display data. So far, I tried this. But its not working
var displayUploads = function(data){
var uploads = document.getElementById('uploads'),
anchor,
x;
console.log(data);
if(data.file === undefined)
{
for(x=0;x<data.length;x=x+1)
{
console.log(data[x].error;
}
}
else
{
for(x=0;x<data.length;x=x+1)
{
anchor = document.createElement('a');
anchor.href = '<?php echo base_url(); ?>'+ data[x].file;
anchor.innerText = data[x].name;
anchor.target = '_blank';
uploads.appendChild(anchor);
}
}
}
Try replacing this line:
if(data.file === undefined)
with this:
if(typeof(data.file) === 'undefined')