Posting a form with hidden fields without submit button click - javascript

I have a form as
<form action="" method="post">
<input name="Descripcion" type="hidden" value="" id="Descripcion" runat="server" />
<input id="Submit1" type="submit" value="Comprar" />
Instead of clicking on submit button i want that the form should be posted without clicking submit button with hidden fields

You can submit an html form from javascript by calling the form's .submit() method. e.g.:
document.getElementById('myform').submit();
Of course, you still need an action in your example so the form has somewhere to submit itself to. Also, you tagged your question asp.net. If this is a webforms page you should use the default form rather then adding your own form to the html markup. You submit the asp.net form by calling the __doPostBack() method.

you can build and submit a form with javascript you can call from other events or when loading a page
myform=document.createElement('form');
myform.method='post';
myform.target='_top';
myform.action='';
input1=document.createElement('input');
input1.type='hidden';
input1.name='Descripcion';
input1.value='';
myform.appendChild(input1);
document.appendChild(myform);
myform.submit();

You can also accomplish the same using jQuery:
$('myform').submit();

Related

How to prevent ajax form submission written anywhere?

I am developing a plugin based on another pro plugins in WordPress.
I would like to stop an ajax form submission which is written in another plugin. I can't change ajax code in another plugin.
Current Scenario
When I submit a form, it will take me to the new page but with that it also running an ajax request. I want to stop that ajax request. Simply, I just want to submit my form normally with new page.
HTML
<form id="redirect_form" class="abc-form" method="post" name="New Form" action="http://example.com/form-redirect">
<label for="form-field-field_1" class="abc-field-label">Test</label>
<input type="text" name="form_fields[field_1]" id="form-field-field_1" placeholder="Enter a location" autocomplete="off">
<button type="submit" class="form_redirect_to">Submit</button>
</form>
jQuery
jQuery(document).ready(function($){
jQuery('#redirect_form').on('submit',function(e){
e.preventDefault();
e.stopPropagation();
$(this).off("submit");
this.submit();
return false;
});
});
As you mentioned it by comment, the default Jquery event is bind using the class of the form.
By changing the default class, the event won't be triggered anymore in the default Jquery function.
Change:
<form id="redirect_form" class="abc-form">
By:
<form id="redirect_form" class="another-class">

Any attributes to ensure an HTML form can't be submitted?

I want to server-render an HTML form in such a way that it is not submittable until it has been asynchronously enhanced by my JavaScript.
It looks like there's no disabled attribute for the form element (MDN).
I could add a disabled attribute to the submit button (and then later remove this with JavaScript when ready), but the user could still submit the form by focusing any input and pressing Enter.
Is there any way to prevent submission without JavaScript (short of just hiding the form entirely in the server-rendered HTML, and unhiding it with JS)?
use type="button" attribute to your submit button and then change it to type="submit"
You can simply do
onsubmit="return false"
on the form tag:
<form onsubmit="return false">
<label>input
<input type="text" name="input" name="a" />
</label>
<input type="submit" value="submit" />
</form>
Sorry, answering my own question - it turns out it's easy to make a form unsubmittable (in Chrome 69 at least) just by disabling the submit button.
When the only submit button is disabled, then even focusing a text field and pressing Enter does not submit the form.

jquery submit form with button on event change

when a checkbox is checked, i want the form to submit. However I need parameters contained in my submit button to be part of the request.
This bit of script submits the form but not using the button. I guess because jquery submits it some other way.
$(e.target).find("input[type='radio']").attr("checked", true)
$(".edit_booking").submit()
I've tried pointing jquery to the button containing the params via it's ID and using a click event, but this doesn't work either.
$(e.target).find("input[type='radio']").attr("checked", true)
$("#bookings_next").click()
Bits of the form:
<form novalidate="novalidate" class="simple_form edit_booking" id="edit_booking_9486" action="/venues/plymouth/bookings/9486" accept-charset="UTF-8" method="post">
.............
<input type="submit" name="forward_button" value="Next step" id="bookings_next" />
Many thanks
aha, simple!
$('#bookings_next').trigger('click');
I have to trigger the event.

How can I submit div data to MySQL

I was wondering how can I submit div data to MySQL. Im not used to javascript so I dont really know whats happening on the javascript part but how can I get or input the action="" part and method="" part and can I or should I add value="" to the hidden input???
Form html code:
<form onsubmit="document.getElementById('hidden_data').value=document.getElementById('showing_data').innerHTML;">
<input id="hidden_data" name="data" type="hidden"/>
<div id="showing_data" class="commenttext" contenteditable="true"></div>
<input type="submit" Value="Enter" id="submitthis">
</form>
Use the hidden field inside the form tag and use the JavaScript to put the value inside it. You can get the hidden field in the $_POST['hydName'].Put the data on the click of the submit button into the hidden field. Keep your action and method of the form same as required. After the click event is fired, it will submit the form to its action URL
<input type="submit" onclick="document.getElememtById('hidden').value = document.getElementById('div').innerHtml;" />

How to submit a form OnKeyPress with Javascript?

I want to make a form like this, and i want to post the form - with javascript - in all the keydowns.
<form action="{$formaction}" enctype="multipart/form-data" method="post">
<input type="text" name="n">
<input type="password" name="pw">
<button name="in" type="submit">enter</button>
</form>
please tell me how to do this.
<form onkeydown="this.submit();">
<!-- form content -->
</form>
<body onkeydown="document.forms["myform"].submit();">
If you do that, the page will reload, just as if you were clicking the submit button.
What you probably want is to attach an onkeydown handler to the password field and submit key presses via AJAX.
For an example look at one of javascript auto-suggest libraries, e.g. AJAX Auto Suggest.

Categories