"form action" and "form submit onclick" to be both executed - javascript

I am writing a basic HTML Form which interfaces with JDBC to pass on the values to the DB.
Here are two lines of my code :
<form id="splash" action="http://localhost:8080/Practice01/DatabaseAccess" method="post">
<button type="submit" onclick="myFunction()" class="btn">Submit</button>
The current output is, when i submit the form, the pade is directed to http://localhost:8080/Practice01/DatabaseAccess. But i want to open another page on submit (or execute myFunction). A thank you page. How do i achieve this.
Any suggestions would be helpful.

create a code behind file to perform database operation and the redirect the code behind page to the page you want to open.
for example:
<form id="splash" action="<codebehindpage>" method="post">
<button type="submit" onclick="myFunction()" class="btn">Submit</button>
in the
response.redirect(<page you want to open>)

Related

triggering a form onsubmit using an anchor

I have a form which has a cancel button, a save (submit) button and a save continue link. Now the buttons work and the javascript is called via a function called from the form onsubmit method. This works as expected as does the cancel button. however the save and continue is not working. Follows is a simplified example:
<form id="save-form" class="admin-form" method="post" action="save-job" onsubmit="return jobFormSave(this);">
<input type="text" name="data" value="">
← cancel
<input class="button" type="submit" value="save">
<a class="button oklink" onclick="document.getElementById('save-form').submit();">ok →</a>
</form>
And the javascript for simplification just alerts ok (the real example has an ajax call to the save-job action):
<script>
window.jobFormSave = function (aForm) {
alert( "Ok" );
}
</script>
So when i click Save button the form is validated, saved and a message is displayed. Clicking on the Ok link however triggers the form submit action (save-job) but i want it to use the jobFormSave function. So I've a few questions:
can this be done while submitting the form data to the function?
can i determine whether i clicked the ok or the save button from within the javascript (or if not within the save-job action function)?
Is there a better way to achieve what seems an everyday thing.
I'm using php 7.2 to handle the forms action functionality and jQuery is available and used for the ajax query in the actual code.
I've kept the code short as mostly it is not relevent to the question but I can provide more code if you guys need it.
Probably you are looking for:
<script>
window.jobFormSave = function (aForm, action) {
alert('User '+ action + " form with data "+ JSON.stringify(aForm))
if (action == 'okayed')
aForm.submit();
}
</script>
<form id="save-form" class="admin-form" method="post" action="save-job" onsubmit="return jobFormSave(this, 'submitted');">
<input type="text" name="data" value="">
← cancel
<input class="button" type="submit" value="save">
<a class="button oklink" href="javascript:{}" onclick="return jobFormSave(document.getElementById('save-form'), 'okayed');">ok →</a>
</form>
Form can be passed as an argument as I did in anchor tag's onclick and then it can be decided to submit at function level based on the action

POST Data to webpage using cURL

I have read many QA regarding posting data to website search field and press submit button.
I followed below solutions but still no luck.
Example1Example2Example3
I want to POST data to for example this website which search box and button code as follows,
<div id="search">
<input type="search" class="text" value="Enter model or part number" id="searchterm_head">
<button class="blue-header-search" type="submit" id="searchbutton_head">Search</button>
<a onclick="if ($('.nav-menu').is(':visible'))
{$('.nav-menu').slideUp(200).fadeOut(200)}
else {$('.nav-menu').slideDown(200).fadeIn(200);};"
class="nav-menu-icon">
<img alt="" src="/assets/images/nav-menu-icon.png">
</a>
</div>
But this code is not included in any form and values are submitted by javascript/jquery.
So is this possible to pass value this kind or search box using cURL
I want to fetch product page url and its content.
Any guidance would be appreciated.
Thanks a lot
Update
I think I found form tag, but there is no value in action attribute.
<form id="MasterForm" action="" method="post">

How to load php form submit on the same page in div

I have got Catalog and search bar(katalog3.php) in php , but funcionality in others files (search bar funcioanliy is search_example.php and catalogue funconality is example_show_model_detail.php) for now they display in funcionality page .... but i want the results of this be display(in katalog3.php) in concrete div(class="results") . How Can i achive it?
<form method="post" id="form1" action="example_show_model_details.php">
<button type="submit" form="form1" value="Submit">Submit</button>
</form>
<form method="post" id="fname" action="search_example.php">
Wyszukaj<input type="text" name="duren" id="duren"><br>
<button type="submit" form="fname" value="Submit">Submit1</button>
</form>
</div>
<div class="results">
</div>
From what I am reading it seems like you are looking for an ajax call to send data to another script and receive the response. If you are using jquery you can read more about it here: http://api.jquery.com/jquery.ajax/. In the event you are not using jquery you may want to take a look at this post: How to make an AJAX call without jQuery?.

How can we send a GET request to a python bottle server without using Javascript?

This is the simple HTML page with a text box and a search button
<!DOCTYPE html>
<html>
<body>
<input type="text" name="queryString"><br>
<button type="button" name="searchButton">Search</button>
</body>
</html>
Initially, the above HTML page is served by a bottle server (I am going to omit the code), and then I wish to process the search queryString using:
#route('/', method = 'GET')
def processSearch():
queryString = request.forms.get("queryString")
print 'queryString is:', keyword
return 'processSearch() executed'
My question is: without a Javascript onclick listener function (which would make an AJAX call), is it possible to click on the search button and fire a GET request which starts executing processSearch()?
Note: I am aware POST is ideal for form data submission, but I am still interested to know if this will work.
Put it in a form, and change the button to a submit input:
<form action="yourscript.py" method="get">
<input type="text" name="queryString"><br>
<input type="submit" name="searchButton" value="Search">
</form>

Clicking submit button of an HTML form by a Javascript code

I don't know much about WEB probramming, so feel free to ask if I'm missing any details.
There is a certain website which I'm visiting very frequently, and it requires users to log in every time they visit. For the login page of this website, I'm trying to write down a userscript which will automatically log me in.
I managed to fill in the form fields, but don't have any idea how to click the submit button by JavaScript. The below is a condensed version of the original login code. How can I automatically click this submit button in this code?
<div id="start">
<div id="header">
<div id="login">
<form id="loginForm" name="loginForm" method="post" action="#">
// ...
<input type="submit" id="loginSubmit" onclick="changeAction('submitInput','loginForm');document.forms['loginForm'].submit();" value="Log in" />
// ...
</form>
</div>
</div>
</div>
The usual way to submit a form in general is to call submit() on the form itself, as described in krtek's answer.
However, if you need to actually click a submit button for some reason (your code depends on the submit button's name/value being posted or something), you can click on the submit button itself like this:
document.getElementById('loginSubmit').click();
document.getElementById('loginSubmit').submit();
or, use the same code as the onclick handler:
changeAction('submitInput','loginForm');
document.forms['loginForm'].submit();
(Though that onclick handler is kind of stupidly-written: document.forms['loginForm'] could be replaced with this.)
You can do :
document.forms["loginForm"].submit()
But this won't call the onclick action of your button, so you will need to call it by hand.
Be aware that you must use the name of your form and not the id to access it.

Categories