Stop page refresh on button click in spring boot - html

I am having a form and in that checkbox and button is present, on click of button, the data are submitting and also inserting in database, but my page is refreshing, I want my data should submit and insert in database and also page should not refresh in thymeleaf spring boot. The values in checkbox are displaying in loop. My current code is:
<form class="myform myform-newaccount" action="#" id="myForm"
th:action="#{/authorList?buttonValue=pluginlist}"
th:object="${userModifyDto}" method="post">
<input type="checkbox" name="applicationId" th:field="*{applicationIds}" id="myCheck"
th:value="${option.applicationId}" />
<input type="submit" value="send" id="myid"/>
</form>

The default behaviour of server-side rendering technology such as Thymeleaf is that actions always trigger a page refresh. See Form handling with Thymeleaf for more information on that.
That said, there are 2 options:
Use JavaScript (either plain JS or a framework) to do AJAX requests and avoid the page refreshes.
Use hx-boost from the htmx library. This will make it seem like there is no page refresh by intercepting the form submit and doing the request to the server via an AJAX call. See TodoMVC with Thymeleaf and HTMX for more information on that option.

Related

How to make an email with submit button, which triggers a post request

I want to send an email which has a submit button, on click of which, a post request would be triggered.
How can I create such an email?
What kind of code would be required?
The code I have written:
<h1>Show a push button:</h1>
<p>The button below activates a JavaScript when it is clicked.</p>
<br/>
<input type="button" value="Click me" onclick="msg()">
That approach you appear to be trying to take is to use JavaScript. This absolutely will not work. Email clients do not allow JavaScript to execute in HTML formatted email.
You could place a regular form inside the email:
<form action="http://example.com" method="POST">
<button>Submit</button>
</form>
… however, form support in email clients is not perfect.
The safe approach is to ask the user to do a two-step process: use a regular link to a webpage containing the form which the user can load in their web browser and then click the submit button for.
If you don't care about the usual protections, then you could have JavaScript submit the form on that page automatically, or change the endpoint that expects a POST request to expect a GET request and use a regular link in the original email.

posting html form data redirect

I am posting modal data to a back end using HTML post and action attr. The form post fine and updates the backend as well the only thing is after pressing submit the url changes and takes me to my POST url. How can this be stopped? I want to stay on the same page after clicking submit and just have the backend update without being taken to my POST url.
<form method="POST"id="goForm"action="http://post_endpoint"enctype="application/x-www-form-urlencoded">
Name: <input type="text" value="">
<button type="submit value="submit">Send<button>
</form>
To submit an HTML form without reloading the page, you have to use Javascript.
Here is a similar question with answers: Submit form without page reloading

Django HTTP post to same URL

What is the "proper" django way to do an HTTP POST to the same page when a button is clicked in that page?
I've got a django app which contains a page with two different buttons. Each button does a different thing but the results of the button press are returned in JSON format which then gets used to update the UI on the page.
I can obviously have each button submit to its own view and do it that way. But I can also make the page view respond to the button presses and detect whether the request is a POST or not.
Then there is the JSON mixin stuff - is it worth trying to use that somehow?
I've got it all working - I'd just like to know what the "proper" way to do it would be.
Any ideas?
As Mikko Ohtamaa said, common practice is to check in your view which button was pressed. e.g.:
template.html:
<form method="post" action="">
{{ obj_form.as_p }}
<button name="action1" value="1" type="submit">
<button name="action2" value="1" type="submit">
</form>
views.py:
if 'action1' in self.request.POST:
form = Action1Form(request.POST)
elif 'action2' in self.request.POST:
form = Action2Form(request.POST)
Using several forms in a single view is more convenient when you have one HTML form with different actions. If you have separate forms (or don't have them at all), I suggest using separate views for each action.

Capture POST response in HTML page

I am sending a form post to a third party and it is returning a page with Success if the action is done. Once I get the success page, I need to redirect user to a Thank You page. Can somebody tell me how the see if the success page is returned and redirect to another page?
<form name="abc" method="POST" action="third party url" >
<input />
<input />
</form>
... how about submitting the form and receiving response in an iframe? If you can use an iframe you'll be able to detect the change then.
the code goes like this:
<form name="abc" method="POST" action="third party url" >
<input/>
<input/>
</form>
but i am not sure how to capture the response
You won't be able to do this easily with plain javascript. When you post a form, the form inputs are sent to the server and your page is refreshed - the data is handled on the server side. That is, the submit() function doesn't actually return anything, it just sends the form data to the server.
If you really wanted to get the response in Javascript (without the page refreshing), then you'll need to use AJAX, and when you start talking about using AJAX, you'll need to use a library. jQuery is by far the most popular, and my personal favourite. There's a great plugin for jQuery called Form which will do exactly what it sounds like you want.

How to use the form value in the action?

I am trying to make a form in html that uses the value you enter to form the destination URL.
<form action="../search/${params.q}" method="post" id="q">
Busqueda: <input type="text" name="q" /><br />
</form>
This is what i am doing, but it does not work, any cluess? thanks!
You'd need to handle this using a script - either server-side or on the client (JavaScript).
HTML alone can't handle parameters in the way you're using them.
So you'd need to either POST the form (as you're already doing) and handle the postback by redirecting your request to the new address, or use JavaScript to capture the field's value when a submit button is clicked and loading the new address in the browser window.
I'd suggest server-side is the best option as JavaScript might be disabled or unavailable.