Two forms on one page, can you prevent refresh when submitting one of two forms?












1















My Java/Springboot/Thymeleaf app has a page with multiple forms. One form contains a modal that pops up when you click a button, so you can attach files. When you hit submit to attach the file, it gets saved to a file on the server, but refreshes the original form and I lose my form inputs. Any way to prevent the default so it doesn't refresh causing loss of data from other form (that hasn't been submitted yet), and still submit to get the file uploaded to my server?



HTML:






    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





Controller:






@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





Ajax Javascript:






$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});












share|improve this question




















  • 1





    I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

    – TinyOS
    Nov 21 '18 at 0:58











  • @TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

    – Stacie
    Nov 21 '18 at 18:19
















1















My Java/Springboot/Thymeleaf app has a page with multiple forms. One form contains a modal that pops up when you click a button, so you can attach files. When you hit submit to attach the file, it gets saved to a file on the server, but refreshes the original form and I lose my form inputs. Any way to prevent the default so it doesn't refresh causing loss of data from other form (that hasn't been submitted yet), and still submit to get the file uploaded to my server?



HTML:






    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





Controller:






@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





Ajax Javascript:






$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});












share|improve this question




















  • 1





    I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

    – TinyOS
    Nov 21 '18 at 0:58











  • @TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

    – Stacie
    Nov 21 '18 at 18:19














1












1








1








My Java/Springboot/Thymeleaf app has a page with multiple forms. One form contains a modal that pops up when you click a button, so you can attach files. When you hit submit to attach the file, it gets saved to a file on the server, but refreshes the original form and I lose my form inputs. Any way to prevent the default so it doesn't refresh causing loss of data from other form (that hasn't been submitted yet), and still submit to get the file uploaded to my server?



HTML:






    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





Controller:






@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





Ajax Javascript:






$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});












share|improve this question
















My Java/Springboot/Thymeleaf app has a page with multiple forms. One form contains a modal that pops up when you click a button, so you can attach files. When you hit submit to attach the file, it gets saved to a file on the server, but refreshes the original form and I lose my form inputs. Any way to prevent the default so it doesn't refresh causing loss of data from other form (that hasn't been submitted yet), and still submit to get the file uploaded to my server?



HTML:






    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





Controller:






@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





Ajax Javascript:






$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});








    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





    <div id="modal" class="modal" data-izimodal-title="Upload a Document">
<div id="newRequiredDocForm">
<form enctype="multipart/form-data" th:action="directBind" method="post" th:object="${document}">
<div class="row">
<div class="col-xs-4 col-sm-3 text-right"><label class="modalLabel">Type:</label></div>
<div class="col-xs-4 col-sm-3 text-right">
<label class="modalLabel">File:</label>
</div>
<div class="col-xs-8 col-sm-7">
<input type="file" id="file" name="document" multiple="multiple" style="margin-right:-20px;"/>
</div>
</div>
<br/><br/>
<div style="text-align: right;"><input type="button" id="attachTheDoc" class="btn btn-docModal" value="Submit"/></div>
</form>
</div>
</div>





@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





@RequestMapping(value="/directBind_ajax", method=RequestMethod.POST)
public @ResponseBody Document docSend(@ModelAttribute("document") Document document, Model model, @RequestParam("file") MultipartFile file){
document.setStorage(storageService.store(file));
model.addAttribute("newDocument", document);
return document;
}





$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});





$('#attachTheDoc').click(function(){
var data = $('#file').val();
console.log($('#file').val());
$.ajax({
type: "POST",
data: data,
cache: false,
url: "/directBind_ajax",
success: function(data){
alert("Your files have been saved");
},
error: function(){
alert("Error - File not uploaded");
}
});
});






java ajax spring-boot thymeleaf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:20







Stacie

















asked Nov 21 '18 at 0:36









StacieStacie

859




859








  • 1





    I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

    – TinyOS
    Nov 21 '18 at 0:58











  • @TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

    – Stacie
    Nov 21 '18 at 18:19














  • 1





    I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

    – TinyOS
    Nov 21 '18 at 0:58











  • @TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

    – Stacie
    Nov 21 '18 at 18:19








1




1





I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

– TinyOS
Nov 21 '18 at 0:58





I think you can find your reponse her : stackoverflow.com/a/31552397/4373948

– TinyOS
Nov 21 '18 at 0:58













@TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

– Stacie
Nov 21 '18 at 18:19





@TinyOS thanks! this is helpful... still a little stuck though, because I want to send a file to a server, not save it, getting an error when trying to post. Will edit my original post to show what I have. Anyone know why the ajax post isn't working for me?

– Stacie
Nov 21 '18 at 18:19












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403701%2ftwo-forms-on-one-page-can-you-prevent-refresh-when-submitting-one-of-two-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403701%2ftwo-forms-on-one-page-can-you-prevent-refresh-when-submitting-one-of-two-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?