如何修复此代码以在上传后显示正确的消息?

时间:2012-09-26 16:19:02

标签: php javascript jquery

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

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

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

2 个答案:

答案 0 :(得分:1)

错误是什么?

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

正确的语法是:

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

你有额外的活动!

答案 1 :(得分: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");
}