I am using multiple form in a page here is my HTML code:
<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>
<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>
<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>
And here is my javascript code:
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>
When i click on submit link, form is not submitting and gererate error in backend:
TypeError: document.myform.submit is not a function.
What may be issue?
Thanks
I think you should use unique ids for all the forms. Then you can have a function that takes as a parameter the id of the form that you want to submit and then submit it. So you can have something like <script type="text/javascript">
function submitform(formid)
{
document.getElementById(formid).submit();
}
</script>
You may try this
<script type="text/javascript">
function submitform()
{
document.getElementById('myform').submit();
}
</script>
You can also use document.forms["myForm"].submit();.
In this case "myForm" is the name of each form and it has to be unique as well as id.
First of all you've got 3 id which are the same (myform), an id has to be unique.
the id of a form must be unique, try to change the form id to only name.
you can try to use document.getElementById('formId').submit() to submit the information of a form.
Related
I want to post data to a PHP file by link.
I'm using this:
<a onclick="$.post('responses.php', {'form':'<?php echo $_GET['form'] ?>'});return false;" href="#">
This code works but I want to post data to and brows response.php then I need a non AJAX equivalent code.
Seems like form should be good for your case:
<form method="post" action="responses.php">
<input type="hidden" name="form" value="<?php echo $_GET['form'] ?>'});return false;"/>
<input type="submit" value="submit"/>
</form>
This code will both post data and redirect user to response.php as you want.
If you don't want an AJAX thing, you can do the same thing using a form:
$(function () {
$("a").click(function (e) {
e.preventDefault();
document.bleh.submit();
});
});
.hidden {display: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="bleh" class="hidden" target="ifr" method="post" action="responses.php">
<input type="submit" />
</form>
<iframe name="ifr" class="hidden"></iframe>
Click to Submit
how can I add a onsubmit="return selection()" function to a form using javascript?
Please see here: JSFIDDLE
id="product_configure_form"
Go with JavaScript-Event-Binding and use event.preventDefault() instead of approaching onsubmit="return selection()"
$('#product_configure_form').on('submit', function(e) {
e.preventDefault();
alert('Form submission is prevented..');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="add-to-cart clearfix" action="http://www.photographycoursetour.com/cart/add/46111438/" id="product_configure_form" method="post">
<input type="text">
<input type="submit">
</form>
I currently have a button in HTML with the following code:
<form id="tfnewsearch" method="get" >
<input type="text" id="search_query" name="q" size="21" maxlength="120"><input type="button" id="search_button" name="search" value = "Search"onclick="doSearch(this.form.q)">
</form>
The function 'doSearch()' works only if I click the submit button. What changes do I have to do if it has to work even if I just press the Enter key?
<form id="tfnewsearch" method="get" onsubmit="doSearch()" >
Simply change the onclick to an onsubmit and attach it to your form!
The proper way it to move it to simple JS script
<script type="text/javascript">
var form = document.querySelector('#tfnewsearch'),
query = form.querySelector('[name="q"]');
form.addEventListener('submit', function(){
doSearch(query.value);
});
</script>
I have multiple forms in my php file for different buttons. So, if I click on Back button, ramesh.php script should be called and so on. This is the code.
<form action="ramesh.php">
<input type="submit" value="Back" />
</form>
<form action="process.php" method="post">
<input name="rep_skyline" type="text" />
<input type="submit" />
</form>
<form action="update.php" method="post" >
<button type="submit">Update</button>
</form>
However, I need to pass some data to server from my client side on form submit just for the update button. I have a javascript function to send the data to server side as below.
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(e) {
var mydata = 3;
if ($(this).is(':not([data-submit="true"])'))
{
$('form').append('<input type="hidden" name="foo" value="'+mydata+'">');
$('form').data('submit', 'true').submit();
e.preventDefault();
return false;
}
})
})
</script>
If I click on the update button, the javascript function is working fine. However, if I click on Back or Submit button, I should not be calling the javascript function. Is there someway to do this?
Give your form an id:
<form action="update.php" method="post" id="update-form">
Then use a more specific selector:
$("#update-form").submit(function() {
// Code
});
I'm not quite sure why you need JavaScript to dynamically add data to your form, however. You should just use an <input type="hidden" /> directly.
type=submit will always load the form's action. Try to specify wich form to submit.
<form name="backForm" id="backForm" action="ramesh.php">
<input type="submit" value="Back" />
</form>
<form name="form2" id="form2" action="process.php" method="post">
<input name="rep_skyline" type="text" />
<input type="submit" />
</form>
Now you can access the form via document.backForm or document.getElementById("backForm") and than use submit(); like document.getElementById("backForm").submit();
I have a form
<form method="post" id="ff">
<input type="name" id="aa" />
<input type="name" id="bb" />
Submit
</form>
Is it possible to post using the href ? I need to post the datas to href url.Is it possible?
Can someone help?
I dont want to use the normal action=
You can try this
<form method="post" action="http://www.example.php" id="ff">
<input type="name" id="aa" />
<input type="name" id="bb" />
Submit
</form>
<script>
function submitForm(){
$('#ff').submit();
}
</script>
Why don't you use
<form method="post" id="ff" action="http://www.example.php">
...
</form>
Yes, you can do this, but it will require JavaScript:
Submit
You could also write more complex JavaScript functions and call that.
Specify the target URL as the action attribute of the form:
<form method="post" action="http://www.example.php">
Strictly speaking, what you ask is possible but not pretty:
<a href="http://www.example.php"
onclick="var e=document.getElementById('ff').action=this.href;e.submit();return false">Submit</a>
I'd go for the action instead...
relace:
Submit
with:
Submit
<script type="text/javascript">
function frmsubmit(obj){
var url = obj.href;
document.getElementById('ff').action = url;
document.getElementById('ff').submit();
}
</script>
if you have more than one tag than use id and find it rather than by tag name