0

In following code i am submitting form via submit but from javascript and the response is going to other page, is there any way to get that response on same page so that i can replace that content to somewhere using jquery.

// controller side code

 @RequestMapping(value = "upload", method=RequestMethod.POST) 
    public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
    @RequestPart("dataFile") MultipartFile file
    ){ if(file!=null){
    //code to upload file and will return 1
            return "1";
        }
        return "0";//otherwise it will return 0
    }

Following is html side code

<html>

<script language="Javascript">
function fileUpload(form) {  
    // Submit the form...
    form.submit();
// here if i get that response then i'll be able to put that content somewhere else via jQuery. i dont want response directed on url/upload.

}
</script>

//html code 
<!-- index.php could be any script server-side for receive uploads. -->
<form target="upload_iframe" method="post" action="upload" enctype="multipart/form-data" encoding="multipart/form-data">
<input type="file" name="dataFile" id="dataFile" /></br>
<input type="button" value="upload"
        onClick="fileUpload(this.form); return false;" >
<div id="uploadShow"></div>
</form>

</html>
b22
  • 293
  • 5
  • 10
  • 22

1 Answers1

0

jQuery's ajax functions should do the trick...

HTML

<div id="content">
  <form id="myform">
  <input type="text" name="myfield" />
  <input type="submit" value="go" />
  </form>
</div>

JQuery

jQuery(document).ready(function($){
  $('#myform').on('submit',function(e){
    //prevent form redirection, natural submit...
    e.preventDefault();
    //post to your php handler...
    $.post(HTTP_PATH_TO_PHPFILE,$(this).serialize(),function(resp){
      // replace content with response..
      $('#content).html(resp);
    });
  });
});

NOTE: this is in response to the general question... "In following code i am submitting form via submit but from javascript and the response is going to other page, is there any way to get that response on same page so that i can replace that content to somewhere using jquery."

Doing the file upload itself is a different animal..:)

jsuna
  • 332
  • 2
  • 4