How to use submit button to go to a URL with HTML - html

I am only starting to learn to code with HTML, so if this question seems trivial or simple, I apologise in advance.
Suppose I have a form, like
<form><input type="url" name="url"><input type="submit" value="Go"></form>
How do I make the submit button go to the url that the user types in?

You cannot do that using pure HTML. The form will always post/get to the URL which the action attribute of the form points to.
However, with some javascript you can do this.
This should work:
<form id="form" method="get">
<input type="url" name="url" onchange="document.getElementById('form').action = this.value;">
<input type="submit" value="Go">
</form>
What this does is it uses the onchange event of the url input box so everytime that changes, the action of the form is updated.

In addition to the sending the user to the url when they hit submit, are you trying to save the url that is typed in?
If so you will need more than HTML to accomplish this, probably php + sql would be the easiest route to save it.
However, if all you're trying to do is let a user go to the url they are typing in, you could accomplish this through javascript or jquery.
For example in your html:
<form>
<input id="url" type="url" name="url">
<input type="button" value="Go" />
</form>
Then add this jquery:
$('input[type=button]').click( function() {
var url = $('#url').text();
$(location).attr('href', url)
});
Try this: http://jsfiddle.net/p6zxg25v/2/

Related

Formaction attribute is not working when input text has required attribute

I have this code, I use formaction attribute to return in home.html
but it's not working because of required attribute.
<form action="post">
Name:
<input type="text" name="name" required>
<br>
Email:
<input type="email" name="name" required>
<button name="Send" id="send">Send</button>
<button name="Return" id="return" formaction="home.html">Return</button>
</form>
The formaction attribute working fine. I can use the Network tab in my browser's developer tools to observe that when I click Return (in the live demo in your question) the form is submitted to home.html.
The required fields are still required (so I have to fill them in before that happens), but that is to be expected.
It sounds like your goal is to provide an exception and not need the user to enter any data when submitting the form to Return.
That isn't possible without adding a bunch of JS but you're approaching the problem from the wrong angle in the first place.
It looks like you want something for the user to click on that will abort filling in the form and just go to a different URL. There's no data submission involved.
That isn't a job for a submit button.
Use a link instead.
Return
You can apply CSS if you want it to look like a button, but I wouldn't recommend it. The visual appearance of the button implies that the form data will be sent somewhere, and that isn't what you are doing.
You should refer to homepage at the form tag
<form action="home.html" method="POST">
and for the submit
<input type="button" name="Return" id="return">

How can I make an html button that passes a parameter?

I want a html button with parameter functionality.
new/?sorting=desc is the url that it should link to. But when I try, the parameter is not passed. How should it be done? I tried both the methods below but none worked.
<FORM METHOD="GET" ACTION="./?sorting=desc">
<INPUT TYPE="submit" VALUE="Äldst först">
</FORM>
I want buttons that behave like these links:
Äldst först
Nyast först
If you want something to act as a link, then you should use a link.
That said:
When you submit a GET form, the query string is removed from the action and a new one is generated from the data in the form.
You need to store the data in hidden inputs.
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="desc">
<input type="submit"
value="Nyast först">
</form>
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="asc">
<input type="submit"
value="Sort the other way">
</form>
If you are using jQuery, you can use the code below.
This will fill a hidden input with the correct value when you click on one of the submit buttons.
<form method="get" action="./">
<input type="hidden" name="sorting" id="sorting" value="" />
<input type="submit" value="Äldst först" id="sort_desc" />
<input type="submit" value="Nyast först" id="sort_asc" />
</form>
<script>
$('#sort_desc').click(function(){
$('#sorting').val('desc');
});
$('#sort_asc').click(function(){
$('#sorting').val('asc');
});
</script>
I think its not possible. A form is used to send data (mostly) via POST or GET. Your goal is to open a specific URL. I would create a standard and would style it like a button. Whats the reason you want to use a button?

Simple HTML forms action

Hopefully a simple question here. If in HTML I have a form like this:
<form name="f_input" action="Test="+num target="TargetFrame" method="get">
Set Number: <input type="text" name="num" value="10">
<input type="submit" value="Submit">
</form>
When I click the button it goes (in the separate TargetFrame frame) to "Test=?num=10". I want it to go to "Test=10", how do I do that?
HTML forms do not provide that capability.
The closest you could get would be:
action="/"
and
name="Test" value="10"
which would give you: /?Test=10
If you really want just Test=10 then you would have to use JavaScript to capture the submit event of the form and override the normal form behaviour. This adds a dependancy on JavaScript so is not a good idea. It would be better to modify the server side of the program to handle the standard form submission schemes.
document.querySelector('form').addEventListener("submit", customSubmission);
function customSubmission(evt) {
evt.preventDefault();
document.frames.TargetFrame.src = "Test=" + encodeURIComponent(this.elements.num.value);
}

how to: question mark in form action

I want to use a button to link to a page. However, the page url is something like domain.com/page?action=register.
I really need this action added to my:
form action="domain.com/page?action=register"
Form attribute, but when I try it with these settings, it will only go to domain.com/page
I've tried encoding the ? into %3F but that doesn't work.
Any help?
The ? values are set by the form, as long as the form's method is set to "get" (rather than "post", which doesn't submit the values in the URL). So, if you want to submit a form to page?action=register, you'd do the following:
<form action="domain.com/page" method="get">
<input type="hidden" name="action" value="register">
It will also pass the other form values along in the URL, creating something like:
domain.com/page?action=register&first_name=john&last_name=doe
EDIT: As #ninetwozero mentioned in a comment, the scenario you describe above should work:
<form action="domain.com/page?action=register" method="post">
[rest of form]
I just tested the above and it passed both the ?action=register and the form values from my form. So, whichever you prefer.
Is "domain.com/page" the page that has the form and button?
Make sure that your button is like this:
<input type="submit" />
Not
<input type="button" />
Otherwise, you could actually use input type "button" with a javascript redirect.
<input type="button" onclick="location.href="domain.com/page?action=register" />
In fact, why use a bunch of form elements if all you want is a button to send you to another url?
<form>
<input type="button" onclick="location.href="domain.com/page?action=register" />
</form>
Not sure if you even need the tags really. Try it.

URL building for login

I am working on a Django project, in which a template login.html has code like -
<form name="input" action="welcome.html" method="get">
--------------SOME FORM ELEMENTS-----------------
<input type="submit" value="Login" />
</form>
When I click on Login, I want URL to be -
http://127.0.0.1:8000/welcome.html
or
http://127.0.0.1:8000/welcome.html?xxxxxxxxxxxxxxxxxx
but url comes out to be
http://127.0.0.1:8000/login.html/welcome.html?xxxxxxxxxxxxxxxxxx
(that is, "action" from form gets appended to the URL instead of getting appended to the domain) where "xxxxxxxx" stands for query submitted through form.
How can I achieve this?
<form name="input" action="/welcome.html" method="get">
You should set the action to be "/welcome.html".
thats really wired, try making your action="/welcome.html"