簡體   English   中英

如何修復此代碼以在上傳后顯示正確的消息?

[英]How do I fix this code to display the correct message after an upload?

我在下面的代碼中檢查文件是否成功上傳,失敗或取消:

  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;   
  }

但是我有一個JavaScript函數,該函數在PHP腳本中將其輸出如下:

<script language="javascript" type="text/javascript"> 
    window.top.stopImageUpload(<?php echo $result ? 1 : 2; ?>, '<?php echo $_FILES['fileImage']['name'] ?>'); 
</script> 

現在,如果文件成功上傳,則顯示正確的消息,如果取消,則會顯示正確的消息,但問題是,如果文件上傳失敗,則顯示取消消息,而不顯示上傳消息時的錯誤。 我試圖更改上述代碼的一部分,因此將如下所示:

...<?php echo $result ? 1 : 2 : 3 ?>..

但這給我一個錯誤。 我的問題是如何更改以上代碼,以便在文件上傳失敗時能夠輸出正確的消息?

有什么錯誤?

您的三元語法不正確: http : //davidwalsh.name/php-shorthand-if-else-ternary-operators

正確的語法是:

echo ( <condition> ) ? <event if true> : <event if false>;

您還有其他活動!

$.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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM