Hiding And Submitting A Form Using jquery - javascript

How to Hide a form and submit that form using jquery?
Is this possible?

Yes, it is possible:
On your HTML page:
<form id="my-form">
</form>
Submit
Your script:
$(document).ready(function() {
$("a#submit").click(function() {
$("#my-form").hide();
$("#my-form").submit();
});
});
If your form contains a Submit button and you want the form to be hidden when the Submit button is pressed, instead you can listen to the submit event and handle it like this:
$("#my-form").submit(function() {
$(this).hide();
return true;
});

What are you trying to do? Some scam?
You can place the form in a hidden div and using $(document).ready event, you can autosubmit the form.

Do you mean a field within a form that already has data inserted, eg. hard-coded in by you, the developer?
If this is the case, just set an id to the input field, with the value hard-coded in. Then set it's display to 'none'. Use your Jquery to interpret the data as normal.
You could also just make a variable in your jquery script, and avoid all this.

Since you've added the jquery-ajax tag, I guess you want to submit the form through AJAX. In that case you are probably looking for something like this:
$("#your-form-id").submit(function(){
$.ajax({
type: "POST",
url: "some.php",
data: $(this).serialize(),
success: function(){
$("#your-form-id").hide();
}
});
return false;
});

Related

click and mousedown events don't work on Ajax form

Simple Ajax popup with a form collecting 3 fields of data, and a submit button. I can't programatically trigger the submit button though.
jQuery('#FormSubmit').click()
doesn't work. Neither does
jQuery('#FormSubmit').mousedown()
nor
document.getElementById("FormSubmit").click();
A real mouse click on the button works, but programmatically triggering it doesn't. It either posts the form in a non-AJAX way (page reload with post data appended to the url) or does nothing.
Anyone know the reasons why?
Is there something specific to Ajax to prevent this?
Edit:
Just tested triggering the submit event on the form instead, and that also posts the form in a non-AJAX way.
You need to use submit, or if you want you can write something like this:
`$('#formSubmit').on('click', function (event) {
event.preventDefault();
var input1 = $('#someInput1').val().trim();
var input2 = $('#someInput2').val().trim();
$.ajax({
url: 'someUrl',
type: 'POST',
success: function (result) {
//do something with the result
}
})
})`
try to use trigger, like this
jQuery('#FormSubmit').trigger("click");

POST form with jQuery and handle the result

Is it possible to POST a form through jQuery and also handle the result? (without using json, ajax etc).
Something like this:
<form id="loginform">
//some input fields and a submit button.
</form>
and then from jQuery:
$("#loginform").submit(function (e) {
$.post("somePHPscript")
.done(function (response) {
//Handle response
});
});
.. or would that equal removing the form, and just binding an event to the submit-button, and take the inputs manually?
I'm not exactly sure why you would want a form that handles the submission result by overriding the default form action and not using ajax etc.
You will want to read this: http://api.jquery.com/submit/ which will outline how to capture the form submission event, you can prevent the move to another page by using the event.preventDefault() as outlined in the above link.
The code you have will handle the response. You just have to do something with it. If you're returning a string of text, you can do something like this:
.done(function(response){
alert(response);
});
If you are returning some HTML, maybe something like this:
.done(function(response){
$(response).insertAfter('div');
});
EDIT
Of course, if you submit the form, then there is no point in trying to retrieve a response from the server. But right now your code is submitting the form and trying to do an AJAX request. Might as well stop the form from submitting and doing the AJAX request so you can do something with the response.
<form id="the-form" role="form">
<input type="button" id="button-form">
</form>
$("#button-form").on('click', function (e) {
e.preventDefault();
$.post($("#the-form").attr('action'), function(response) {console.log(response)});
});

Adding .addClass to jQuery submit form

I have created a jQuery submit form using
$(document).ready(function(){
var $form = $('form');
$form.submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
$('.rsvp-left').addClass('rsvp-hide');
$('.rsvp-right').addClass('rsvp-hide');
$('.rsvp-success').addClass('rsvp-show');
},'json');
return false;
});
});
And I cannot seem to get the .addClass to work within this code.
The rest of the script works great, and the form submits properly without leaving the page, but the class changes will not execute.
I am trying to change the classes so that I can show that the form has been submitted correctly.
Any ideas on how I can make it work?

Posting New html from TINYMCE

Seems like a simple problem, I have a form and when someone needs to edit data, the textarea that is controlled by TINYMCE loads the values, but when I change it and submit the form, the new changes are not being posted.
What am I doing wrong?
UPDATE
How do I do it via this, or do it say on click in the editor. I am using jquery validate, this is the submit handler.
$(form).ajaxSubmit({
target:'#result',
success:function(){
$('html, body').animate({scrollTop:'90px'}, 500);},
clearForm: false});
}});
You have to call tinyMCE's save method when the user clicks the submit button:
$(form).ajaxSubmit({
beforeSerialize: function() {
tinyMCE.get('content').save(); // 'content' is the id of your textarea
},
...
});
Reference: http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.Editor/save

Problem with disabling submit buttons on form submit

I am using the jQuery disable on submit plug-in but I have a problem. If I disable the submit buttons they don't get passed back to the server so I cant tell which button was pressed. Is this normal? Is there anything I can do about it?
I really don't want to retool my web site so I have to set a variable on form submission to tell which button was pressed.
Any ideas?
How to Disable the Submit Button of a Web Form
This method hides the button instead of disabling it, and programmatically inserts a disabled <button> tag to make it appear just like the submit button was disabled. Works great.
Here's a workaround I just found in a jQuery forum:
<script type="text/javascript">
$(document).ready(function() {
$("#sendSearch").click(function() {
$('#loadingDiv').show();
$('#sendSearch').attr("disabled", "disabled");
// these two lines are the workaround
this.form.submit();
return true;
});
});
</script>
You could do the submit via jquery and disable the button afterwards:
<input type="submit" value="do it!" onclick="$('#mainform').submit(); $(this).attr('disabled','disabled' ); $('#pleasewait').show();" />
EDIT:
I forgot form.submit() is not asynchronous. You can do an ajax request instead:
$.ajax({
url: "someurl",
type:"POST",
cache: false,
dataType: "json",
success:gotIt,
async:true,
timeout:240000,
error:ajaxError,
data:$("#mainform").serialize()
});
or you could just .hide() the button, or after clicking it setting a non-functional onClick() handler and styling it to look disabled.
Be more simple :)
var formid="#id-form-if-exists"; //Put here the id if exists
$("form"+formid).submit(function(){$("form"+formid+" input").attr("disabled",false);});
YEAH

Categories

Resources