Forcing a POST on every checkbox/dropdown change - javascript

How can I force any change to a checkbox (inside a form) or to a drop-down menu selection to cause a HTTP POST to be issued by the browser?
Bandwidth is not an issue, page reloading is not an issue and I don't want to go the full AJAX route.
What I really want is an HTTP POST to be done when the user clicks on a checkbox (or selects something from a drop-down menu), etc. without the user having to click 'Submit' after its change.
Maybe it should be done with some JavaScript on the client-side? (I couldn't succesfully Google anything)

Use Javascript. Add onchange="document.getElementById('myFormId').submit()" to the elements, or do this programatically. myFormId must be replaced by the HTML id of the form element.

You could use the JavaScript onchange event to then call the submit() function on the form.

if you have a form already set up just put a class to the element you want to use as trigger and then
for select
$(".classname").change(function(){
$("#formid").submit();
});
for checkbox/radio
$(".classname").click(function(){
$("#formid").submit();
});

AFIK it can't be done without client-side scripting. The easiest way would be to trigger the submit event for every form change. With jQuery it's done with the following code:
<script type="text/javascript">
$(function() {
$("input[type=checkbox],input[type=radio],select").change(
function(evt)){evt.target.form.submit();}
);
});
</script>
If you don't use jQuery, you'll have to write some boilerplate event handling code. See this introduction for more info on JavaScript event handling.

Related

Autoclick a form input?

I am trying to create an adhoc code that save me pressing a repetitive button on a webpage, I opened F12 developer and tried using getElementbyID(..).click(); however didn't quite work, this is part extract of the button, I wonder if someone can advise what code I can use to automatically submit the button? Any help will be greatly appreicated. Best regards, Jon
what code I can use to automatically submit the button
If this is for a regular form post without AJAX then you can select the form then use submit().
E.g.
/*
* The variable myform is previously determined such as by `document.forms[0]`
* or a similar DOM selector method
*/
myform.submit();
If this is for a non-form button then use the click() method as suggested in the comments above. However, be aware that for this method there would have to be an Event Listener attached for the button to do anything of value.

Use JavaScript to disable all buttons on a page until a refresh

I have a page that will cause an error if a user tries to click too many buttons at one time (for the impatient user) and therefore need to DISable any button (all defined by a JS onclick function) on the page until it is refreshed (with new data sent via the server using Java.) What is the best method to do this, and is there a way to do it with jQuery?
You would have to find all types of buttons using something like this..
$('input[type="submit"], button')
and loop through the returned array and do .attr('disabled','disabled'); on the item in each iteration.
How about simply calling this when you want to disable the buttons:
jQuery('input[type="button"]').attr('disabled', 'disabled');
That will disable all inputs of type button on the page. Of course, as soon as you reload/replace the page contents, the new buttons will not be disabled. You can also just disable all inputs if that's easier.
Fiddle: http://jsfiddle.net/duffmaster33/xDMux/
The single best solution is to use the BlockUI plugin for jQuery. It accomplished everything I needed and more. http://www.malsup.com/jquery/block/

jquery trigger action on option add

I have an select field which is on a jsp page and I add options to it through an external js library. Now is there a way on my jsp to trigger an action when an option is added?
Its because I need to get some value from backend and its not possible to do everything on an external js page.
I used .change() but I guess because of the fact that the adding takes place in an external page, it does not trigger anything.
Use .on
$('#your_dropdown_id').on('change', function (d) {
// your code here..
});

trigger 'click' after selected an option?

I have select box which load addresses form via ajax. So user can select previous saved address. Inside address form , another select box which lists 'States'. A shipping is calculated based on 'States' select box change.
I want to trigger a change after loading addresses. I used this code
$('select#addressed').change(function() {
$('select#states').trigger('change');
});
But this will trigger change before new address load.Any way to trigger after loading address?
Fire the change in the success section of the Ajax call. That way it won't fire until the data comes back. You could also use a $.when/.done as well.
http://api.jquery.com/jQuery.when/
You can fix this by putting the "click" trigger in the complete function. I'm assuming from your syntax and tag you're using jQuery. Have a look at this link under "complete".

Acknowledgement on form submit

I want to show a acknowledgement (more precisely a popup) when form is successfully submitted.Previously I was using Ajax to validate form and display pop up but now I want to achieve same without Ajax.
Is there any event in javascript/Jquery which is invoked after successful form submission? or Is there any other alternative available?
Thanks!
EDIT 1 :
I am using Spring 3.0.
Here is the detailed scenario
1. User fill the form and click on submit
2. Request will be sent to controller (Server side)
3. Validation will be done at server side
4. If errors are present I am using Spring validation to show it and goto Step 1
5. else successfully submit the form and show a popup.
6. After user clicks on popup redirect to other page.
EDIT 2:
I am completely agree with the opinion that Ajax is the right/best way to do it and I already implemented it using Ajax. But client want to use non-ajax approach and I cannot go beyond his words.
This question piqued my curiosity, as I was trying to do something similar using the iframe solution suggested by Leon. Eventually I succeeded, however, I would like to suggest that rather than using a direct onload property, you make use of the jQuery .load() event on the iframe.
Edit: So here's how I set up the form (using HTML5, so quotes aren't necessary):
<div id=message></div> /* Example-specific, see below */
<form method=post action=backend.php target=iframe>
// Form data here
</form>
<iframe name=iframe></iframe>
I added the following CSS code to hide the iframe:
iframe {
border:0px;
height:0px;
visibility:hidden;
width:0px;
}
Don't use display:none, as some browsers will refuse to submit to an element that's not displayed.
Then in my $(document).ready() JavaScript...
$('iframe').load(function(){
// Your load event here.
});
You could also change that about, so that it specifically only triggers after a specific event (if you're using dynamic forms, for example). In such a case, you may want to use .unbind('load') before .load() to prevent previously-added .load() functions from calling.
Now when the form is submitted, it loads into the hidden iframe. When the iframe loads the page (backend.php, in my example), it triggers the .load() function. In my specific case, I set up a <div id=message> to display a message:
$('iframe').load(function(){
$('#message').html('The form successfully submitted.');
});
Without Ajax? No Problem - let's go back to how the Web really used to work in the past ;-)
Since I am getting you don't want to refresh the current page, how about this approach:
have a hidden iframe on the same page, with a name & id
point the target property of your form to the name given in the previous step
submitting the form will now be "hidden"
you can have an onload property on the iframe set to a javascript method of your liking to get called once the form finished submitting
that javascript code could also retrieve the contents of the iframe and check for your server-side response (maybe even including an error msg)
notify the user about the result
This is all fairly easy to setup, let us know how it works for ya..
I am not sure which language you are coding in.
One option - use javascript.
On the submit button onclick event (client side event), perform the page validation and display alert pop up, if the page is valid.
<script type="text/javascript">
function OnSubmitClientClick() {
Page_ClientValidate();
if (Page_IsValid) {
alert('Form has been successfully submitted.');
return true;
}
}
</script>
Why do you want to drop AJAX approach? Without AJAX, server side validation implies page reload. On page reload you would lose client side (JS) state.
One alternative is to use hidden frame/iframe/a new window to perform server side validation on form submit(possibly use the pop up you are referring to in your question). Which in my opinion is not the right approach(A BIG NO). You may rather stick to AJAX or go with non AJAX way of form submit.

Categories