0

i have a situation where i'm stuck at the idea of catching the appropriate error during file upload in the ajax response in jquery form-plugin.

i'l give an idea of what i want to achieve through some pseudocode.

My php is :

 $file = strtolower($_FILES["myfile"]["name"]);
 $extension = substr($file, strrpos($file, '.') + 1);
 if($extension == 'jpg'){
  // upload the file
 if(move_uploaded_file($_FILES["myfile"]["tmp_name"], $folder . $finalFilename)){
  // do something here like
 echo "<div id='statusContainer'>
<table><tr><td>
<img src='uploads/".$finalFilename."'>
</td></tr>
<tr><td>".$finalts."</td></tr></table>
</div>";
       }
  } else {
  $error = 1; // will give numbers to different errors like filetype error, size error etc..
  }

now my JS code is :

 (function() {
 var status = $('#status');   
 $('form').ajaxForm({
complete: function(xhr) {
if(xhr.responseText == "// what do i get echoed here so i can run an array and show user appropriate error like size error, type error etc. // "){
     // i want to open a dialog box with correct error message//
} else{
    status.hide().html(xhr.responseText).fadeIn(1000);
    }
  }
   }); 

 })(); 

shall i get the error number echoed in the ajax response and run through an array to get the message? but then i'll have to put in a lot of if conditions in the ajax response with different error numbers.

Please anyone have a more logical idea??

2 Answers 2

1

you could make an array and pass error json_encode()'ing it and parse json response from ajaxForm, like php part:

$responseArr = array();
if( file_is_uploaded ) {
  $responseArr["error"] = "0";
  $responseArr["message"] = "File uploaded success message";
}
else {
  $responseArr["error"] = "1";
  $responseArr["message"] = "Error message here";
}
echo json_encode($responseArr); //pass it as response

js part::

$('form').ajaxForm({
  dataType: "json",
  success: function(response) {
      //parse json response and perform accordingly
      console.log( response );
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

what about the html response i'm echoing on success of upload. will mentioning datatype as "json" and then getting error response as json and success as html wud conflict?
tried with different suggestions on other questions, put together all the pieces and BAM, it works like a charm... thanks sudhir...
0

To cut down on traversing an array in JS, and matching error IDs to descriptions, could you associate the error description itself to be relayed in the ajax response? This would keep your JS lean and keep error handling serverside.

Example:

<tr><td>".$finalts."</td></tr></table>
</div>";
}

$error_descriptions = array("Filesize Error", "Extension Error");

} else {
$error = error_descriptions[1]; 
}

3 Comments

how will i differentiate an error from a success html response into the ajax response block?
if your success html response is static, then code your javascript to output the success string if the error-description is empty.
sorry, i'm not following you. could u modify my JS function and give an idea of what you are suggesting me to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.