document.getElementById("myForm").submit() is not working [duplicate] - javascript

This question already has answers here:
"Submit is not a function" error in JavaScript
(19 answers)
Closed 5 years ago.
<form name="formName" id="formName" action="" method="post" autocomplete="off">
<input type="hidden" name="text1" id="text1" value='0' />
<input type="button" name ="submit" onClick="Submit()" value="submit">
</form>`
<script>
function Submit() //my function
{
document.getElementById("formName").submit();
}
</script>
I need some help... I have also tried `this document.formName.submit();` but still not working.
on debugging i got this error :Uncaught ReferenceError: formNameis not defined

JavaScript code goes into a <script> tag, otherwise it is just rendered as text and is not interpreted as code.
<form name="formName" id="formName" action="" method="post" autocomplete="off">
<input type="hidden" name="text1" id="text1" value='0' />
<input type="button" name ="submit" onClick="Submit()" value="submit">
</form>`
<script>
function Submit() //my function
{
document.getElementById("formName").submit();
}
</script>
JavaScript is also case sensitive so "Submit" is not the same as "submit".
And you should never name a form input "submit", read: "Submit is not a function" error in JavaScript.

Your javascript code isn’t in tag. What you are going to do is taging and put your code between tags.
<script>
Put Your codes here
</script>
And change submit() to Submit() in onClick

Related

Submit form automatically [duplicate]

This question already has answers here:
Auto submit form on page load
(5 answers)
Closed 4 years ago.
hey can anyone help me
I would like to automatically submit the form on page load is there anyway it could be done?
<form method='post' action='/delete-account.php'>
<input type="submit" id="submit" name="submit" value="Confirm Account Removal" /></form>
<form method='post' name="myForm" id="myForm" action='/delete-account.php'>
<input type="submit" id="submit" name="submit" value="Confirm Account Removal" /></form>
window.onload=function(){
document.forms["myForm"].submit();
}
You can submit this way by using the jquery
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
document.getElementById("myDataForm").submit();
});
</script>
</head>
<body>
<form method='post' id="myDataForm" name="myDataForm" action='/delete-account.php'>
<input type="submit" id="submit" name="submit" value="Confirm Account Removal" /></form>
</body>
</html>
Please try this.

How to clear form elements that are pre-populated with $_POST values

If you run and submit the form below:
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
First name:<br>
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])){echo $_POST['firstname'];} ?>"
<br>
<input type="submit" value="Submit">
<input type="button" onclick="this.form.reset()" value="Clear Form">
</form>
</body>
</html>
Why does this.form.reset() not work when the form is pre-populated with the previously submitted value? The Clear Form button only works before submitting data. I've tried a number of similar approaches using jQuery as well that also only work before submitting the form, never after.
Since JavaScript runs after server-side code, I would expect the value from $_POST['firstname'] to be replaceable with JavaScript (in this case, an empty string).
As suggested by #Taplar reset will set value which was initially set while page load. Don't get confused with "Reset" form value Vs setting value to "" (Blank string)
Try this:
HTML:
<input type="text" id="fn" name="firstname" value="preloaded value" />
<input type="button" onclick="clear_form();" value="Clear Form">
JAVASCRIPT:
function clear_form() {
document.getElementById('fn').value = "";
}

How to submit form when have input type="submit" inner form using javascript? [duplicate]

This question already has an answer here:
JavaScript form submit WITH a field called submit
(1 answer)
Closed 6 years ago.
How to submit form when have input type="submit" inner form using javascript ?
i want to submit form using javascript (have input type="submit" inner form ).
i tried to use this code
<form action="" method="post" name="test_fn">
<input type="text" name="input1" value="ON">
<input type="submit" name="submit" value="ON">
</form>
<script type="text/javascript">
setTimeout(function(){test_fn.submit();}, 0);
</script>
but not work. how can i do that ? for submit form using javascript (have input type="submit" inner form )
thank
First you should change your input submit name.
When you name the button submit, you override the submit() function on the form,so it will become "submit is not a function"
<form action="" method="post" name="test_fn" id="test_fn">
<input type="text" name="input1" value="ON">
<input type="submit" name="btnsubmit" value="ON"> //Changed submit name here
</form>
then can call submit by your form name like this
<script type="text/javascript">
setTimeout(function(){document.test_fn.submit()}, 0);
</script>

Uncaught TypeError: document.getElementById(...).submit is not a function

Why doesn't the following code work?
<html>
<body>
<form id="myForm" action="http://example.com" method="POST">
<input type="text" name="engine" value="v2.5" />
<input type="text" name="verify" value="2" />
<input type="text" name="submit" value="Save" />
</form>
<script type="text/javascript">
document.getElementById("myForm").submit();
</script>
</body>
</html>
I'm getting the following error:
Uncaught TypeError: document.getElementById(...).submit is not a function
Thanks.
I want to send a POST parameter named submit through forms, how can this be done automatically?
myForm.submit is a reference to the text input with name="submit":
<input type="text" name="submit" value="Save" />
Change the name to something other than "submit".
Since you have an input with the name submit, try to use following line:
document.getElementById("myForm").submit.click();
Rename <input type="text" name="submit" value="Save" /> to something other than submit, and it should work.
This is because you can access form values using object notation in Javascript, so by calling submit(), you are attempting to execute the submit field as a function.
However, your question is how to send a POST parameter named submit. You might want to take a look at XMLHttpRequest, like this:
xmlhttp.open("POST", "http://example.com", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("engine=v2.5&verify=2&submit=Save");
go to submit button and check if you put id = submit EX:
if your id was submit you should change it to other id that's all

How create a button click on intut [duplicate]

This question already has answers here:
Submit two forms with one button
(7 answers)
Closed 8 years ago.
I have two different forms. Each of them has its own submit button
<input type="submit" id="edit-submit" name="op" value="Submit" class="form-submit ajax-processed">
and
<input type="submit" id="edit-submit--2" name="op" value="Submit" class="form-submit ajax-processed">
I need to make a separate button that would click on these two input. How can I do this?
If you have more than 2 forms and only want 2 of them to be submited, they must have a common part, like a css class (no styling here, only a selector).
HTML part :
<form id="form1" class="myForm ..."/>
<form id="form2" class="myForm ..."/>
<input type="button" id="myButton" value="Submit both forms"/>
js :
$("#myButton").click(function(){
$(".myForm").submit();
});
You can add as many form as you want, if they have the class myForm, they will be submited too. No need to change your js code...
Just provides the ids to your form.for example:
<form id="form1" class="myForm ..."/>
<form id="form2" class="myForm ..."/>
<input type="button" value="Click Me!" onclick="submitForms()" />
<script>
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
}
</script>

Categories

Resources