image submit button passing value - button

I have one image submit button like this
<input id="enter" name="enter" type="image"
value="Login"
src="images/btn_login.jpg"/>
once i click on this image i got following values
enter.x=39&enter.y=11
but i want to get enter=Login on submit
how to get enter value as login on submit?
Thanks
Some Quick browser tests
Chrome: Working
Safari: Working
Firefox: Working
IE: Not Working
Opera: Not working
Working means enter=login is passed. All browsers are at latest version at the time of writing

IE6 and IE7 definitely don't pass through the value of an image submit, so you can't use this method if you want your page to work in those browsers. However I'm surprised that you say the latest version of IE doesn't work, I thought it did
The only reliable fallback I've found is to overload the name attribute:
<input id="enter" name="enter.login" type="image"
value="Login"
src="images/btn_login.jpg"/>
Then, you have to iterate through all the form params and look for one whose name starts with 'enter'. Note that your input still needs to have a value attribute of some kind, otherwise browsers won't pass it through at all.
Alternatively you can use a normal submit button with a background image - tricky to get consistently styled, or a <button> - although that has even more issues in IE than an image submit :(

You can use a small javascript for this that passes the right value to a hidden input field on clicking the image and submit the form.
Example:
<script>
function submitimg(myvalue)
{
document.getElementById('enter').value = myvalue;
document.getElementById('myform').submit();
}
</script>
<form id="myform">
<input type="hidden" name="enter" value="">
<img src="images/btn_login.jpg" style="cursor:pointer;" onclick="submitimg('Login');">
</form>

Related

add 'success' message to html form

I have an HTML form I'm custom coding that integrates with a drip (email platform) form. And I'm trying to get it to show a "success" message (e.g. "thank you for signing up to our newsletter".
What would be the best/cleanest way be to adapt the HTML to allow that message after a submit action?
Here's my code so far:
<form class="subscribe-form" form action="https://www.getdrip.com/forms/0123456789/submissions" method="post" data-drip-embedded-form="0123456789">
<div style="width:25vw">
<input class="subscribe-form__input" type="email" id="drip-email" name="fields[email]" placeholder="Email" value="" >
</div>
<button class="subscribe-form__submit" type="submit" data-drip-attribute="sign-up-button">Sign Up</button>
</form>
Thanks!
Start off by creating the message and styling it properly. Maybe something like this...
<form class="subscribe-form" form action="https://www.getdrip.com/forms/0123456789/submissions" method="post" data-drip-embedded-form="0123456789">
<div style="width:25vw">
<input class="subscribe-form__input" type="email" id="drip-email" name="fields[email]" placeholder="Email" value="" >
</div>
<button class="subscribe-form__submit" type="submit" data-drip-attribute="sign-up-button">Sign Up</button>
</form>
<p class="subscribe-form__thanks">Thanks for subscribing!</p>
You could even wrap the thank you in a div if you would like and add a "thumbs up" icon to fill the space.
Once you're happy with your design, add this to you CSS (if you're using SASS/SCSS, you can add it nested within the element):
hide {
display: none;
}
and add that class to your "Thank You" message, like this:
<p class="subscribe-form__thanks hide">Thanks for subscribing!</p>
Now that that's all set up, you simply need to use JavaScript to remove the hide class from the "Thank You" message, and add it to the form, which will reveal the message and hide the form.
I'll use JQuery for brevity, but Vanilla JS will work great too!
$(".subscribe-form__submit").onClick(()=>{
$(".subscribe-form").addClass("hide");
$(".subscribe-form__thanks").removeClass("show");
});
That should all be working as desired - the form should disappear and the message should appear! The animation could be a little jarring, so have a play around with fading then hiding, and matching the height of the two divs to avoid the page having to change size.
This will hide the form, even if the fields are incorrect/incomplete, so you could look into validate.js to improve your usability if you're interested.
NOTE: This method of using the onClick() JQuery selector doesn't guarantee that the user is actually subscribed to the mailing list - your Drip API request could be incorrect, or their API could fail/be offline.
You can look into the Drip API's callback function (https://developer.drip.com/) if you're interested in making sure the user is properly subscribed, however there's no guarantee they will reply in a timely fashion, and so you'd most likely be over complicating things.
Hope this helped!!

autocomplete="off" not working for Google Chrome

This question has been asked Several times in the past but unfortunately there's no way i could disable autofill for Google Chrome (v.36.0.1985.125 m)
I have already Tried
"AutoComplete=Off" not working on Google Chrome Browser
How do I stop Chrome from pre-populating input boxes?
how to disable google chrome suggestion list when using twitter bootstrap typeahead?
Code tested so far
<form autocomplete="off">
<asp:textbox autocomplete="off">
AutoCompleteType="Disabled"
But I still get autofiled data on my login page. Passwords are populated even if Textbox Ids are changed.
Since I have been having this very same issue and it seems all "alternatives" stopped working as the browsers have been getting recent updates, I came across a "dirty" solution that baffled me completely.
If we go on each field we want to disable the autocomplete feature and set its autocomplete attribute to an invalid value (only "off" and "on" are valid), the browser will stop trying to autofill those fields because of that.
Surprised? So was I!
Example:
<input type="text" autocomplete="stopdoingthat" />
And apparently it works. Stupid, I know, but it's a "dirty" one that actually works for now.
Recent Version of Google Chrome are forcing Autofill irrespective of the Autocomplete=off . You are going to need little bit of hack here. Some of the previous hacks don't work anymore (34+ versions)
I have tested following code on Google Chrome v36.
It removes "name" and "id" attributes from elements and assigns them back after 1ms. This works perfectly in my case.
Put this code in document ready.
$(document).ready(function () {
$('form[autocomplete="off"] input, input[autocomplete="off"]').each(function () {
var input = this;
var name = $(input).attr('name');
var id = $(input).attr('id');
$(input).removeAttr('name');
$(input).removeAttr('id');
setTimeout(function () {
$(input).attr('name', name);
$(input).attr('id', id);
}, 1);
});
});;
For Asp.Net TextBox adding TextMode="Search" and AutoCompleteType="Disabled" worked for me
<asp:TextBox runat="server" ID="txtAddress" CssClass="form-control" TextMode="Search" AutoCompleteType="Disabled"></asp:TextBox>
and i tested this in both Edge (Version 89.0.774.48) and Chrome (Version 88.0.4324.190 )
This readonly-fix worked for me:
fix browser autofill in: readonly and set writeble on focus (at mouse click and tabbing through fields)
<input type="password" readonly
onfocus="$(this).removeAttr('readonly');"/>
By the way, some more information on Chrome and Safari auto fill behaviour:
Sometimes even autocomplete=off would not prevent to fill in credentials into wrong fields, but not user or nickname field. I guess, the Chrome and Safari look for a password field to insert your saved credentials. Then it autofills username into the nearest textlike-input field , that appears prior the password field in DOM (just guessing due to observation). As the browser is the last instance and you can not directly control it, but the read-only trick above fixes it. :-)
Figured I'd update this with a 2016 post. I used a combo of both Sangram's solution, as well as dsuess'. This is working for me as of the time of this post.
Sangram's solution unfortunately no longer worked for me with a later Chrome version, but using dsuess' method of marking the fields as readonly, while not visually appealing (since they stay readonly until you actually focus on the element, which isn't the most intuitive way to do it - but whatever works, right?), did still work.
By combining the two, we no longer require the user to focus on the field. Here is my web page code:
<asp:TextBox ID="TeamName" runat="server" TextMode="SingleLine" AutoCompleteType="Disabled" ReadOnly="true"></asp:TextBox>
Which, for the curious, after being parsed by IIS, renders to the following raw HTML:
<input name="ctl00$MainContent$uc_CreateTeam$TeamName" type="text" autocomplete="off" id="MainContent_uc_CreateTeam_TeamName" readonly="readonly">
The accompanying js:
// Disable autocomplete for form fields. This is done by setting the 'autocomplete' attribute (which is deprecated)
// as an indicator that we want this field to not be autofilled. We also set these forms to load as readonly (which
// will cause browsers to ignore them), and now we mark them writable.
$(document).ready(function() {
$('form[autocomplete="off"] input, input[autocomplete="off"]').each(function() {
var input = this;
setTimeout(function() {
$(input).removeAttr('readonly');
}, 200); // 100 does not work - too fast.
});
});
Hopefully this helps any stragglers still looking for the solution a few years later!
Look, the only way to fix this is to petition the Chromium developers. Get on their forums and tell them what a pain this is. Every workaround is eventually destroyed by them.
https://bugs.chromium.org/p/chromium/issues/detail?id=914451
As of right now, having a unique ID on the control fixes it.
My recommendation is to use the user's session ID as part of the control ID on the page. That currently works across the board. But it won't forever.
Make type of password field as 'text' and change it to 'password' on click.
HTML:
<input autocomplete="off" id='username'>
<input autocomplete="off" id='password' type="text" onclick="changeType('password')">
js:
changeType(id) {
document.getElementById(id).setAttribute('type', 'password');
}
work for me :
<asp:TextBox ID="TextBox" runat="server" Text='mootez'onfocus="this.autocomplete='off'" ></asp:TextBox>
My suggestion change the name of the control. For example, I had a control named txtFirsName and always it showed me autocomplete suggestions, I changed by txtFN and Ready!

Post form without a button

I was exploring the search box on the Apple website and noticed it doesn't have a input type="submit" to post the form, even when Javascript is disabled.
<form action="/search/" method="post" class="search" id="g-search">
<div class="sp-label">
<label for="sp-searchtext">Search</label>
<input type="text" name="q" id="sp-searchtext" accesskey="s">
</div>
</form>
Having never really explored it, I take it from this it means you can post a form without needing a submit button, it just relies on the user pressing the return key.
Two questions: 1) Is this compatible across all browsers? So in IE 7 will pressing return still work?; 2) Is there a way to do this in ASP.NET without using an asp:button? I will probably have it inside a placeholder (where I would conventionally use defaultButton to allow for multiple forms on the page) but if I can get rid of the button altogether then that's a plus.
yes of course it is possible to do it in anyway you want.
The simpler thing is to have an onclick event that calls a function that does the submit like this:
JQuery:
$('#id_of_form').submit()
javascript:
document.name_of_my_form.submit();
or
document.getElementById('id_of_my_form').submit();
so simple :)

Checking a radio button with jQuery when radio button is runat="server"?

Using jQuery I want to be able to click an element which will also checks it's related radio button. I had this working fine until we had to add runat="server" to the radio buttons.
When I apply this it prevents my jQuery function from working and I cant figure out how to get round it, heres a simplified version of the code:
HTML
<input type="radio" runat="server" id="sector1Radio" name="SectorGroup" title="Sector1" />
jQuery
$('#SomethingElse').click(function() {
$('input[title=Sector1]').attr('checked','checked');
});
I've found out that when its converted to a .net control instead of checked="checked" (as it would be usually) it is just Checked, so I changed that but on inspecting the DOM in multiple browsers, none of my radio buttons are being checked :-(
Are there any other ways I can use jQuery to check a radio button that has runat="server"?
Cheers!
I think that Your problem is that the id of the input is no longer sector1Radio but rather ctl00_sector1Radio or something similar. This happens if Your input control is inside e.g. a ContentPlaceHolder control (when using master pages).
Can You check the generated HTML code (in the browser) to verify if this is the case? What is the id of the input control?
If this is the case, You need to generate Your js jQuery code
$('#SomethingElse').click(function() {
$('input[title=Sector1]').attr('checked','checked');
});
from codebehind so that SomeThingElse is replaced with the ClientID of the control.
.is(':checked') works on ASP.NET radiobuttons and checkboxes
$('#SomethingElse').click(function() {
$('input[title=Sector1]').is(':checked');
});
try using
$('input[title=Sector1]').attr('checked',true);
and
$('input[title=Sector1]').attr('checked',false);
or maybe
$('#SomethingElse').click(function () {
$('input[title=Sector1]').attr('checked',!$('input[title=Sector1]').attr('checked'));
});
As suggested by others, ASP.net will not generate the html with the same ID you specified.
Quick solutions:
You can keep using the id but asks jquery to check the end of the id instead, example:
$("input[id$='sector1Radio']").is(":checked");
Or check against the title and name as Nico suggested
Use the class element which is not effected by ASP.net, example
<input type="radio" runat="server" id="sector1Radio" class="sector1Radio" name="SectorGroup" title="Sector1" />
$("input.sector1Radio").is(":checked");
Best thing is to view the generated html code and see what id is giving you, then you can use the appropriate jquery selector, because the generated id could have different extensions depends whether you use master pages, etc.
If you are using a MasterPage or are creating the controls dynamically then it is probable that the control ID's are being renamed #SomethingElse becomes #MainContent_SomethingElse.
The easiest way to check this is to use the WebDeveloper plugin for Firefox or Chrome.
Go to Information -> Display Element Information and then select the object in question. It will give you it's ID, class, as well as ancestor and children information.
Check to see if the ID is being changed dynamically by the .NET.
If that's the case:
To prevent this, in the server side code you can use the following attribute to create static ID's
SomethingElse.ClientIDMode = ClientIDMode.Static;
You can then reference in you jQuery
$('#SomethingElse').click(function() {
if ($('input[title=Sector1]').attr('checked')) {
//execute event
});
I think what happens is that in ASP NET Checkboxes and Radio Buttons generates an "input" and a "span" after the input. So you need to select the input only.
You can try:
$('.classname input[type=checkbox]').each(function() {
this.checked = true;
});
Two things here: finding the control and executing the check. In ASP.NET, your control's actual ID and name will end up getting changed based on the runat="server" containers in which it appears, even if those containers have no Ids.
Rendered ASP.NET controls always end with the same name as you started with, so a tag like:
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
might end up being rendered as
<input type="radio" runat="server" id="ctl0$ctl0$sector1Radio" name="ctl0_ctl0_SectorGroup" title="Sector1" />
You can find this element, even after it is rendered if you use the "contains" selection syntax in JQuery. So to find this element, once rendered, you could use:
$("input[type='radio'][id*='$sector1Radio']")
This syntax will find any radio button whose id contains "$sector1Radio"
Once you have the element, you can check or uncheck it using the following code, which you'd call from the click event of your other element.
// check the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', true);
// uncheck the radio button
$("input[type='radio'][id*='$sector1Radio']").attr('checked', false);
One last thing... if you just want a block of text to click the button when pressed (wrap it in an tag and set the AssociatedControlId property to the control name of your radio button, like this...
<input type="radio" runat="server" id="sector1Radio" title="Sector1" />
<asp:label runat="server" id="lblsector1Radio" associatedControlID="sector1Radio">clicking here clicks and unclicks the radio button</asp:label>
I had the same problem. To use the jQuery UI to make your radiobuttons nice one has to write:
<div id="radio">
<input type="radio" id="radio1" runat="server" />
<label for="radio1">The label of the radio button</label>
...
</div>
<script type="text/javascript">
$('#radio').buttonset();
</script>
The id of the input tag must be correctly referenced by the label's for attribute. If the webpage is inside a master page then the id of the input tag will be modified to something like ctl00_Something_radio1, and suddenly the label's for attribute no longer references the input tag. Beware of this in ASP.NET!

Handle user hitting 'Enter' key in a ASP.NET MVC web site

I am working on a ASP.NET MVC web site that has multiple submit buttons. i.e.
<input type="submit" name="SubmitButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="submit" name="SubmitButton" value="Back" />
I need to allow users to quickly submitting the form by pressing the 'Enter' key. HTML standard seems to specify that the first submit button will be assumed if a user press the 'Enter' key. However, I need to make the 2nd button (i.e. the "OK") button the default button and for reasons I don't even want to talk about, changing the button order is not an option.
I googled around and I found this post about using Page.Form.DefaultButton in ASP.NET but this doesn't work with ASP.NET MVC.
I also tried the following javascript solution, while it works in Chrome but doesn't work in IE6
$('body').keypress(function(e) {
if (e.which === 13) {
$("input[value='OK']").trigger('click');
}
});
I can think of some really extreme solutions such as going through every single controls in the form an attach the above function to them. However, I don't think that's a very neat solution so I am wondering has anyone got a better solution?
First off, this is wrong:
<input type="submit" name="SubmitButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="submit" name="SubmitButton" value="Back" />
All three of them are submit buttons. A reset is an input of type="reset". Get that sorted. Second of all, I've successfully implemented something like that, and it works on IE6. Try this:
function keypressHandler(e)
{
if(e.which == 13) {
e.preventDefault(); //stops default action: submitting form
$(this).blur();
$('#SubmitButton').focus().click();//give your submit an ID
}
}
$('#myForm').keypress(keypressHandler);
The focus() part makes the button appear to be pressed when the user presses enter. Quite nifty.
Use this.
$(function(){
$('input').keydown(function(e){
if (e.keyCode == 13) {
$("input[value='OK']").focus().click();
return false;
}
});
});
You should only have one submit button. The reset button should be type="reset" and the back button should probably be type="button" like this:
<input type="reset" name="ResetButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="button" name="BackButton" value="Back" />
Then, Reset and OK will just work the way they are supposed to and you'll only need to handle the Back button click with Javascript.
Edit: The other option would be to place the Reset and Back submit buttons each in their own forms inside iframes. Then they would be ignored by the main form and wouldn't be default buttons. Also, this would allow you to point them to different server actions if needed and there wouldn't be any reliance on Javascript for the button actions.
HTML standard seems to specify that
the first submit button will be
assumed if a user press the 'Enter'
key.
No, the usage of the enter key isn't defined, it's a propritary extension that's been added under various interpretations. You will get different behavoir in different browsers (and it can become very dangerous when you start mixing in different cultural or UI conventions about left to right/right to left ordering of options).
If there is only 1 button on the form then all the mainstream browsers happen to follow the same behavior - they submit the form as if that button was pressed (a buttonName=buttonValue is included with the form data). Of course this doesn't mean the buttons onclick handler is going to fire - that behavoir is browser specific.
When there are several buttons it's a complete crap shoot. Some browsers decide that the first button (and the definition of first can vary - most use the first one mentioned in the Html tree, while others attempt to use screen position) was clicked and use it in the submission, while other browsers (notably some versions of IE) make the equally correct assumption that no specific button was pressed, and so don't include a buttonName=buttonValue (the rest of the form is submitted fine).
Since you use jquery, if you use hotkeys plugin, you can make a such approach:
$(document).bind('keydown', 'return', function (evt){
$.next("input[value='OK']").trigger("click");
return false;
});
Change button order in source but not visually (ie, use CSS to swap the buttons)?

Resources