0

我在下面有这段代码,它检查文件是否上传成功、不成功或取消:

  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 ?>..

但这给了我一个错误。我的问题是如何更改上述代码,以便在文件未成功上传时输出正确的消息?

4

2 回答 2

1

错误是什么?

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

正确的语法是:

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

你有额外的活动!

于 2012-09-26T18:10:34.683 回答
0
$.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");
}
于 2017-07-07T07:08:43.400 回答