I have a basic html form with a search box. depnding if the user inputs searchA or searchA-B, I need to redirect him to two different places (url is formatted differently).
How would you suggest I do that?
<form name="ajaxSearchFrm" action="url1 or url2 depending on the value of searchText" method="GET">
<input type="text" id="searchText" name="searchText" style="width:200px;" onKeyUp="searchSuggest();" autocomplete="off">
<input type="submit" value="Search" style="background-color: #fff; font-family: verdana, arial, helvetica; color: black; font-size: 10px;" />
<div id="formSuggestLayer"></div>
</form>
You'll want to use javascript to evaluate the contents of the text box. Depending on the value, change the action of the form then submit the form via javascript. You'll have to change your submit button to a regular button with an onClick event though.
JavaScript:
function EvaluateForm()
{
if( document.getElementById('searchText').value == 'A' ){
//submit to A
document.ajaxSearchFrm.action = "myurlA.com";
}
else{
//submit to b
document.ajaxSearchFrm.action = "myurlB.com";
}
document.forms["ajaxSearchFrm"].submit();
return false;
}
HTML:
<form name="ajaxSearchFrm" action="" method="GET">
<input type="text" id="searchText" name="searchText" style="width:200px;">
<input type="button" value="Search" onClick="return EvaluateForm();" />
<div id="formSuggestLayer"></div>
</form>
Related
I have a form where I need to check for all of the required inputs if are not empty so I can enable the submit button.
<form id="inquiry-component-form" method="post">
<input id="inquiry-media-outlet" type="text" name="media_outlet" required>
<input id="inquiry-name" class="form-control" type="text" name="name" required>
<button id="submit-inquiry-form" disabled type="submit">Submit</button>
</form>
I tried to do this with jQuery the problem is that it is not possible because of Liquid/Jekyll code runs at build time. So I need to know a way to do this with HTML.
Any ideas on how can I achieve what I need?
You can't do this with just HTML, but you can with CSS.
input[type=text]:not(:valid) ~ button[type=submit] {
pointer-events: none; /* Disables mouse pointer interactions */
/* Styles to make it look like it's disabled */
opacity: 0.6;
}
<form id="inquiry-component-form" method="post">
<input id="inquiry-media-outlet" type="text" name="media_outlet" required>
<input id="inquiry-name" class="form-control" type="text" name="name" required>
<button id="submit-inquiry-form" type="submit">Submit</button>
</form>
Explanation:
input[type=text]:not(:valid) ~ button[type=submit] - Select buttons with type submit that are siblings of invalid (:not(:valid)) inputs with type text.
pointer-events Caniuse
:valid Caniuse
You'll also have to add tabindex="-1" to the button if you want to prevent tabbing-and-entering.
Are you sure the js is run after the document is loaded? All template engines support JS. It is by no means that you can't use JS to do that.
document.addEventListener('DOMContentLoaded', e => {
const formEle = document.querySelector('#inquiry-component-form')
const inputEles = formEle.querySelectorAll('input')
const buttonEle = formEle.querySelector('button')
formEle.addEventListener('input', e => {
let filled = Array.prototype.every.call(inputEles, ele => ele.value.length > 0)
if (filled) {
buttonEle.removeAttribute('disabled')
} else {
buttonEle.setAttribute('disabled', '')
}
})
})
<form id="inquiry-component-form" method="post">
<input id="inquiry-media-outlet" type="text" name="media_outlet" required>
<input id="inquiry-name" class="form-control" type="text" name="name" required>
<button id="submit-inquiry-form" disabled type="submit">Submit</button>
</form>
I'm on Home/Index. I have the following HTML:
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
For some reason, if I have the cursor in the ConfirmationCode input and I press the Enter key, the form submits, and redirects to http://localhost:62500/?ConfirmationCode= . The thing is, I've read about this behaviour and I understood it might be somewhat intended behaviour depending on browser and whatnot. But I have this other form,
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="button" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
which doesn't have this behaviour, and nothing happens when I press the Enter key.
The form submits because you hace only 1 input in your form (no additional data needs to be entered).
Change you first form to :
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="text" name="ConfirmationCode2"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
and the form will not submit when you press Enter
If you want to disable the submition functionality of the form you can add the onsubmit event handler like this:
<form id="frmCode" style="display: inline-block" onsubmit="return false;">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
You need to set the type of the button you want to use to submit your form to submit.
<input type="submit" ...>
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
More infos on the input element and on control types.
That is the usual browser behaviour, when a user presses enter when using a form.
The form will submit and as there is no action attribute in your form tag, it will go back to the current URL (with any of the form fields/values attached) ?ConfirmationCode=
As mentioned in 'Ubiquitous Developers' comment; your are using html buttons <input type="button"/> and not the html submit button <input type="submit" />. This will not have the default behaviours of the submit button.
Its probable that the writer of the script you had, didn't want to use the default behaviours of html forms. Instead using javascript to decide how the form will behave.
I think that they use some javascript to prevent the submission of the form like this;
How to prevent ENTER keypress to submit a web form?
The first form is a simple form which follows the default browser rules. The rule is pressing enter means clicking the submit button.
<input type="button"/>
above is not a valid submit button, so the browser submits the form.
In the second case the scenario is diffrerent.
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
here this is a valid submit button and so in pressing enter key, the browser clicks the button and this button instantly calls the JavaScript function "login()". Now it must be in the code that it does something that prevents the form from submission. may be something like "return false;". So the form doe snot submit.
You can try this. it works
<form method="post" action="yourpage.html">
<input type="text" name="name" class="inputbox" />
<button type="submit" id="btn1">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script>
$(".inputbox").on('keyup', function (e) {
if (e.keyCode === 13) {
$("#btn1").click();
}
});
</script>
I have written this html:
Add Comment
<div id="addcomment">
<form name="comment" action="" method="post">
<input type="text" name="addcomment" size="80" />
<input type="submit" value="Comment">
</form>
What I want is when I click the "Add comment" then form should be displayed in place of "Add comment" just like on "stackoverflow". How can I make this happen??
Here's a quick and dirty proof, optimize from here:
CSS:
#addcomment {
display : none;
}
.addcomment:active {
display : none;
}
.addcomment:active + #addcomment {
display : block;
position : absolute;
top : 5px;
}
http://jsfiddle.net/7uax74qd/
Use javascript for it...
Add Comment
<div id="addcomment" style="display:none;">
<form name="comment" action="" method="post">
<input type="text" name="addcomment" size="80" />
<input type="submit" value="Comment">
</form>
THEN IN JAVASCRIPT write a function and call that in anchor tag..
function show()
{
document.getElementById('add').style.display='none';
document.getElementByid('addcomment').style.display='block';
}
I have made this simple search box and want to use an svg image as the submit button, how could I do that without any javascript?
here's the form:
<form method="post" action="#">
<input type="text" name="rechercher" id="rechercher" placeholder="Rechercher"/>
<input type="submit" value="none" onerror="this.onerror=null; this.src='images/loupe.png'"/>
</form>
You can use an image button on the button
<input type=image src=PATH_TO_YOUR_IMAGE alt="Submit Me">
or set submit button background to an image using css
input[type=submit] {
background:url(BACKGROUND_IMAGE_PATH_HERE);
border:0;
display:block;
height:Change_TO_backgroundimageheight;
width: Change_To_backgroundimageWidth;
}
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="image" src="submit.gif" alt="Submit">
</form>
The long and short is input type image will also submit the form
I have a simple HTML form with multiple submit buttons and I want my application to act differently on whichever submit button I click.
Example:
<form method="POST">
<input type="submit" value="test" name="test" />
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search" />
<input type="text" name="search_query2" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search2" />
<input type="submit" value="GO" name="next" />
</form>
This works for me, and I know how to distinguish which button I clicked. The problem I have is that if I push Enter while editing the text-field then the first submit button gets clicked (the one named test). Is this possible to let the browser know (with HTML only - the whole point of this is to make it work with NO JavaScript) that I pushed Enter and it should send search with POST, not test?
So is there some kind of binding of text field to the submit button?
A solution to this problem would be to have multiple form elements, including your different submit buttons and the corresponding textfields.
I can't think of a solution with just one formular but no Javascript in use.
You could move the one you want to be submitted to first in that list, like so:
<form method="POST">
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search" />
<input type="submit" value="test" name="test" />
<input type="submit" value="GO" name="next" />
</form>
But it seems that your HTML is just poorly formed, I'd recommend Merguez's answer
Since your edit, there is no way to do this without Javascript/jQuery.
This would not be hard to do with PHP.
First: Change your buttons to actual controls:
becomes
<form method="POST">
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some
text" />
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Search">Search</button>
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Test">Test</button>
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Go">Go</button>
</form>
Now, when the form is submitted, check the returned value of actionB. If it's "Search", then piece together your search URL using the returned value of search_query.
There is NO way to read variables using only HTML. JavaScript is required if you're not going to use a scripting language to do this. You can totally do this in native JavaScript. JQuery is NOT required for this relatively simple function.
<script language="JavaScript">
function checkBtn() {
var btnX=document.getElementById("actionB").value;
if (btnX=="Search") {
// compose your new URL here
}
// repeat for each of the possible buttons
</script>
Hope that helps somewhat!
Steve