Reputation: 83
I have this code below where it checks to see if a file was uploaded successfully, unsuccessfully or canceled:
if (success === 1){
result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span><br/><br/>';
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
displayInfo = replaceForm;
}
else if (success === 2){
result = '<span class="imagemsg'+imagecounter+'"> The file upload was canceled</span><br/><br/>';
displayInfo = updateForm;
}
else {
result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span><br/><br/>';
displayInfo = updateForm;
}
But I have a JavaScript function where it outputs this below in the PHP script:
<script language="javascript" type="text/javascript">
window.top.stopImageUpload(<?php echo $result ? 1 : 2; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');
</script>
Now if the file uploaded successfully, the correct message is displayed, if canceled, the correct mssage appear but the problem is that if the file fails to upload, then it displays the cancel message and not the error while uploading message. I tried to change part of the above code so it will be like this:
...<?php echo $result ? 1 : 2 : 3 ?>..
But this gives me an error. My question is how do I change the above code so that it will be able to output the correct message for when a file does not upload successfully?
Upvotes: 0
Views: 94
Reputation: 158
$.ajax(
{
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Lists/getByTitle('" + libTitle + "')/Items",
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
'Accept': 'application/json;odata=verbose',
'X-RequestDigest': $("#__REQUESTDIGEST").val()
},
success: fetchSuccess,
error: onFailure
});
function fetchSuccess(r) {
// How to display the count of "r"
console.log(r.length);
}
function onFailure(r) {
alert("Error + r + working");
}
Upvotes: 0
Reputation: 6780
What is the error?
Your ternary syntax is not correct: http://davidwalsh.name/php-shorthand-if-else-ternary-operators
The correct syntax is:
echo ( <condition> ) ? <event if true> : <event if false>;
You have extra events!
Upvotes: 1