How to make a form submit on div click using jquery? - javascript

I already tried this in single php file but doesn't work out, so i tried now in two separate php file one for form and another one for process.
How to submit the form on a div or link click?
Code i tried
$(document).ready(function(){
jQuery('.web').click(function () {
$("#g_form").submit();
alert('alert');
});
});
FORM
<form action="p.php" id="g_form" method="POST">
<input type="text" name="f1" value="">
<input type="submit" value="submit!" name="submit"/>
</form>
<div class="web">click</div>
Here is the process file code p.php
<?php
if(isset($_POST['f1'])){
echo $_POST['f1'];
} ?>
When i click the submit button the form is submitting but when i click the .web div it is not submitting the form even i get the alert message but not submitting.
What wrong am doing here? It'll be helpful if i get a idea.

.submit() docs
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method.
Name conflicts can cause confusing failures. For a complete list of
rules and to check your markup for these problems, see
DOMLint.
You give your submit button a name of submit, which the above passage tells you will cause "confusing failures"
So if you accessed the dom element and looked at the .submit property you would see that since you name the button submit instead of .submitbeing a function its a reference to the buttons dom element
HTML
<form action="p.php" id="g_form" method="POST">
<input type="text" name="f1" value="">
<input type="submit" value="submit!" name="submit"/>
</form>
<div class="web">click</div>
JS
//Get the form element
var form = $("#g_form")[0];
console.log(form.submit);
//prints: <input type="submit" value="submit!" name="submit"/>
And when you change the submit name
<form action="p.php" id="g_form" method="POST">
<input type="text" name="f1" value="">
<input type="submit" value="submit!" name="psubmit"/>
</form>
<div class="web">click</div>
JS
var form = $("#g_form")[0];
console.log(form.submit);
//prints: function submit() { [native code] }
so simply give your submit button a different name that does not conflict with a form's properties.

You can trigger submit button click.
<form action="p.php" id="g_form" method="POST">
<input type="text" name="f1" value="">
<input type="submit" value="submit!" id="f_submit" name="submit"/>
</form>
<div class="web">click</div>
$(document).ready(function(){
jQuery('.web').click(function () {
$("#f_submit").trigger( "click" );
alert('alert');
});
});
DEMO : http://jsfiddle.net/awladnas/a6NJk/610/

HTML (provide a name for the form, strip the name from the submit):
<form action="p.php" name="g_form" id="g_form" method="post">
<input type="text" name="f1" value="">
<input type="submit" value="submit!"/>
</form>
<div class="web">click</div>
JavaScript
//use jQuery instead of $ in the global scope, to avoid conflicts. Pass $ as parameter
jQuery(document).ready(function($){
//use on(), as it's the recommended method
$('.web').on('click', function () {
//use plain JavaScript. Forms are easily accessed with plain JavaScript.
document.g_form.submit();
alert('alert');
});
});

Change the name of the submit and Try,
<input type="submit" value="submit!" name="mySubmit"/>

Remove the submit from the form and try again:
<form action="http://test.com" id="g_form" method="GET">
<input type="text" name="f1" value=""/>
</form>
<div class="web">click</div>
I changed the action to a real URL and the method to a GET so something is seen changing.
Fiddle

$(".web").live('click', DivClick);
function DivClick(){
$("#g_form").submit();
}

Related

jQuery event on input submit field

I want to fade in an element with JQuery code when a inputfield is clicked. However, I cannot seem to get it to work, when the selector is placed inside the form. If I remove the selector outside the form it works fine. Is there any work-aroud for this? Or, maybe JQuery code does not execute inside a form? Here is my code:
<form action="newBet.jsp" method="get">
<fieldset>
<input id="test" type="submit" name="update" value="✎"/>
<input class="form-control" id="div1" style="display:none;" type="number"/>
</fieldset>
</form>
$(document).ready(function(){
$("#test").click(function(){
$("#div1").fadeIn();
});
});
If I place the selector #test outside the form it works fine, but I want to execute the JQuery inside the form.
Your form gets submitted, which refreshes the page and doesn't let you see the result.
Instead of applying your event to the submit button, apply it to the form itself and return false to prevent default behavior (submitting):
<form id="test-form" action="newBet.jsp" method="get">
$(document).ready(function(){
$("#test-form").submit(function(){
$("#div1").fadeIn();
return false;
});
});
<input id="test" type="button" name="update" value="✎" />
type="submit" refreshes the page. You neet to change type="button"
You can get the fadein effect with the input inside the form if you add preventDefault() like this:
$(document).ready(function(){
$("#test").click(function(e){
e.preventDefault();
$("#div1").fadeIn();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="newBet.jsp" method="get">
<fieldset>
<input id="test" type="submit" name="update" value="✎" />
<input class="form-control" id="div1" style="display:none;" type="number" />
</fieldset>
</form>
The default behaviour of the input of type "submit" inside the form is to submit the form which you can prevent using preventDefault() and call at a later time after the fadein effect.

Form submit button not working with AngularJs

I've a problem with my form. I want to make standard PHP form but AngularJS is blocking the "Submit" button.
When I click the "Submit" button, it returns some errors in console. And remember I don't want to dynamically submit.
The error is:
An invalid form control with name='' is not focusable.
This example
<body ng-app="mainApp">
<form action="post.php" method="post">
<div ng-controller="MainCtrl">
<label for="titlex">Title</label>
<input id="titlex" class="form-control" type="text" maxlength="75" min="10" name="titlex" required>
</div>
<input type="submit" value="Send">
</form>
</body>
This issue pops up in different cases:
You have a hidden form element that has a required attribute for validation.
You hide an form element before send your data.
Some required form elements does not have a name attribute.
Your submit input does not have a name attribute.
You can try to add a name attribute to your submit input:
<input type="submit" value="Send" name="send">
or you can setup your form to be not validated by the browser mechanics by using
<form name="myform" novalidate>
Try adding name attribute in input tag.
Only form elements with a name attribute will have their values passed when submitting a form.
<input type="submit" value="Send" name="send">
Hope this solves your problem.

submitting a form via jquery removes submit input variable

Here's my HTML:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="test" method="get" action="">
<input type="hidden" name="var1" value="true" />
<input type="submit" name="var2" value="submit" />
</form>
<script>
$("#test").submit();
</script>
The resultant request that that makes has var1 in it but not var2. My question is why and what can I do to get var2?
Here's a live demo:
http://www.frostjedi.com/terra/dev/submit.php
try: method="post" in form or use <button type="submit"></button>
A form should submit the value of a submit button only if it's clicked to submit the form (see HTML5 4.10.22.4 Constructing the form data set). Calling the submit method doesn't click the button, so it doesn't submit the value.
The code you've posted will endlessly submit the form, thanks.
You can call the click method of the button, but that may not work (i.e. submit the button's value) everywhere:
<form id="test" method="get" action="">
<input type="hidden" name="var1" value="true">
<input type="submit" name="var2" value="submit">
</form>
<button onclick="$('#test')[0].var2.click()">submit form</button>
Or, as user3701524 suggests, use method=post if that suits.
The value on button will only be submitted when u actually CLICK that button. To make it all work nice, I recommend binding to
$('form').on('submit', function(e) {
// here you have the button value if it was clicked.
e.preventDefault(); // optional this prevent default submission.
});
either way, calling $('form').submit() will NOT send the button value because is not triggered by the button itself.
Hope it helps!

How To Submit An HTML Form Using PHP?

This is what I am trying to do without success :
<form name="something" action="ht.php" method="post">
Submit
</form>
When I click on the link I want to post the value helloworld to ht.php. How can I do this?
You can't just do document.url.submit(), it doesn't work like that.
Try this:
<form id="myForm" action="ht.php" method="post">
<input type="hidden" name="someName" value="helloworld" />
Submit
</form>
That should work!
Using jQuery, its rather easy:
$('form .submit-link').on({
click: function (event) {
event.preventDefault();
$(this).closest('form').submit();
}
});
Then you just code as normal, assigning the class submit-link to the form submission links:
<form action="script.php" method="post">
<input type="text" name="textField" />
<input type="hidden" name="hiddenField" value="foo" />
Submit
</form>
I find this method useful, if you want to maintain an aesthetic theme across the site using links rather than traditional buttons, since there's no inline scripting.
Here's a JSFiddle, although it doesn't submit anywhere.
try
<form id="frmMain" action="ht.php" method="post">
Submit
</form>
Try this,
<!-- you need to give some name to hidden value [index for post value] -->
<form name="something" action="ht.php" method="post">
<input type="hidden" name="somename" value="helloworld" />
Submit
</form>
Also try this
<!-- you need to give some name to hidden value [index for post value] -->
<!-- also you can use id to select the form -->
<form name="something" action="ht.php" method="post" id="myform">
<input type="hidden" name="somename" value="helloworld" />
Submit
</form>
You could add a hidden field on the page (set it's name property), set it's value to helloworld.
Then in your hyperlink's onclick call form.submit()

Jquery submit form error

I have a form which I want to submit upon button click which is outside the form, here is my HTML :
<form id="checkin" name="checkin" id="checkin" action="#" method="post">
<input type="text" tabindex="100" class="identifier" name="identifier" id="identifier">
<input type="submit" tabindex="101" value="Submito" class="elsubmito" name="submit">
</form>
Here is my jQuery :
$("button").live('click', function() {
$("#checkin").submit();
});
$("#checkin").live('submit', function() {
});
When I click submit button inside the form its submitting ok, but its not submitting when I click on the button which is outside the form tags, why? how can I fix this ?
You are selecting all the <button> elements but you are trying to select an <input>.
It works when it is inside the form because the the normal submit functionality runs.
Change the selector to match the element you actually have: input[type=submit]
Better yet, forget about the JS and just structure your HTML better so that the submit button is inside the form.
If you're handling the form processing using JavaScript, then you'll want to return false in your button and form processing code.
I was able to achieve identical results using the JavaScript below, and the two HTML examples (with the button inside and outside of the form element).
JavaScript/jQuery
$("button").live('click', function() {
$("#checkin").submit();
return false;
});
$("#checkin").live('submit', function(){
alert("Hello world!");
return false;
});
HTML Example 1
Button inside the form.
<form id="checkin" name="checkin" id="checkin" action="" method="post">
<input type="text" tabindex="100" class="identifier" name="identifier" id="identifier">
<input type="submit" tabindex="101" value="Submito" class="elsubmito" name="submit">
<button>test</button>
</form>
HTML Example 2
Button outside the form.
<form id="checkin" name="checkin" id="checkin" action="" method="post">
<input type="text" tabindex="100" class="identifier" name="identifier" id="identifier">
<input type="submit" tabindex="101" value="Submito" class="elsubmito" name="submit">
</form>
<button>test</button>
As I said, both examples performed as expected. You may want to double-check your button listening code to ensure that you are in fact using the button element. If you're using an element with the id attribute set to button, then you'll want to ensure you are using the proper jQuery selector:
$("#button").live('click', function() { // ...
you can have a simple hyperlink outside of your form like this
click to submit and that's all you need

Categories