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
Related
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.
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.
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.
I am trying to embed the search forms of two other sites into my own site. Here is my site, the forms are at the bottom: http://dsa.dartmouth.edu/.
The DGD search works fine - the user gets directed to the search results page - but the Course Picker one doesn't - the user just gets redirected to a blank search page.
This is my form code:
<form action="http://coursetown.hacktown.cs.dartmouth.edu/search" method="post" class="appSearch">
<input id="search" name="query[title]" placeholder="course title(s)" type="text" role="textbox">
<input type='submit' name='commit' type="submit" value='Search' />
</form>
Is there a way to make it so that the user gets directed to coursetown's search results page when they click search?
The search form on http://coursetown.hacktown.cs.dartmouth.edu/search submits by AJAX so there is no search URL to hit. Furthermore, it seems to use a hidden "authenticity_token" field, which is probably generated server-side on load so you probably need to find out if they have an API you can use for this.
If you can edit that page directly to trigger the search by JavaScript based on URL parameters that would be an option too, not sure what level of control you have over the search 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.