Reputation: 1021
I am trying to submit a form through onclick event on tag. I have tried triggering document.myform.submit(), this.form.submit(), parentNode.submit() etc. but none of this is working! Using a submit button, the code works fine. But I want to use tag in place of that. Need some help.
<form id="myform" name="myform" method="POST" action="process_edit_questionnaire.php?project=<?php echo $project_id; ?>">
<input name="module" type="hidden" value="<?php echo $module_id;?>"/>
<input name="project" type="hidden" value="<?php echo $project_id;?>"/>
<input name="hidden_ques_id" type="hidden" value="<?php echo $data_array_ques[$j]['ques_id'];?>"/>
<div id="question_block">
<div id="serial_block">
<p id="s_original_<?php echo $j+1; ?>"><a href="#" onclick="showSelectBox(<?php echo $j+1; ?>)">Serial :
<?php
if($data_array_ques[$j]['ques_position']==NULL){
echo $j+1;
}
else {
echo $data_array_ques[$j]['ques_position'];
} ?></a></p>
<p id="s_select_box_<?php echo $j+1; ?>" style="display: none;">Serial :
<select name="serial">
<?php for($p=1;$p<=count($data_array_ques);$p++){ ?>
<option value="<?php echo $p; ?>" <?php if($p==$data_array_ques[$j]['ques_position']){ echo "selected=\"selected\"";} ?> ><?php echo $p; ?></option>
<?php } ?>
</select>
</p>
</div>
<div id="question_text">
<a href="#" onclick="showTextBox(<?php echo $j+1; ?>)"><p id="q_original_<?php echo $j+1; ?>"><?php echo $data_array_ques[$j]['ques_text']; ?></p></a>
<p id="q_text_box_<?php echo $j+1; ?>" style="display:none;"><textarea id="ques_text" name="ques_text" rows="3" cols="30"><?php echo $data_array_ques[$j]['ques_text']; ?></textarea></p>
</div>
</div>
<div id="menu_block">
<a href="" onclick="document.myform.submit()">Save Changes</a> |
<a href="delete_question.php?project=<?php echo $project_id; ?>&module=<?php echo $module_id; ?>">Delete This Question</a>
</div>
</form>
Upvotes: 56
Views: 149556
Reputation: 6793
you can do it like this with raw javascript
<html>
<body>
<form id="my_form" method="post" action="mailto://[email protected]">
<a href="javascript:{}" onclick="document.getElementById('my_form').submit();">submit</a>
</form>
</body>
</html>
For those asking why I have a href
element in my anchor tag, its because it's a compulsory part of an anchor tag, it may well work without but it's not to spec. So if you want to guarantee rendering etc you must give the engine a fully formed tag. So the above achieves this. You will see href="#"
used sometimes but this is not always wanted as the browser will change the page position.
Upvotes: 79
Reputation: 5405
Clean and simple:
<form action="/myaction" method="post" target="_blank">
<!-- other elements -->
<a href="#" onclick="this.parentNode.submit();">Submit</a>
</form>
In case of opening form action
url in a new tab (target="_blank"
):
<form action="/myaction" method="post" target="_blank">
<!-- other elements -->
<a href="#" onclick="this.parentNode.submit();">Submit</a>
</form>
Upvotes: 4
Reputation: 41
<form id="myform_id" action="/myMethode" role="form" method="post" >
<a href="javascript:$('#myform_id').submit();" >submit</a>
</form>
Upvotes: 4
Reputation: 135
Using Jquery you can do something like this:
$(document).ready(function() {
$('#btnSubmit').click(function() {
$('#deleteFrm').submit();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" id="deleteFrm" method="POST">
<a id="btnSubmit">Submit</a>
</form>
Upvotes: 2
Reputation: 397
You can use hidden submit button and click it using java script/jquery like this:
<form id="contactForm" method="post" class="contact-form">
<button type="submit" id="submitBtn" style="display:none;" data-validate="contact-form">Hidden Button</button>
<a href="javascript:;" class="myClass" onclick="$('#submitBtn').click();">Submit</a>
</form>
Upvotes: 1
Reputation: 1173
Is there any reason for not using a submit button and then styling it with css to match your other a tags?
I think this would reduce your dependency on having javascript enabled and still get the desired result.
Upvotes: 2
Reputation: 624
If jquery is allowed then you can use following code to implement it in the easiest way as :
<a href="javascript:$('#form_id').submit();">Login</a>
or
<a href="javascript:$('form').submit()">Login</a>
You can use following line in your head tag () to import jquery into your code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
Upvotes: 15
Reputation: 17
I suggest to use "return false" instead of useing some javascript in the href-tag:
<form id="">
...
</form>
<a href="#" onclick="document.getElementById('myform').submit(); return false;">send form</a>
Upvotes: 1
Reputation: 3558
Try this:
Suppose HTML like this :
<form id="myform" name="myform" method="POST" action="process_edit_questionnaire.php?project=<?php echo $project_id; ?>">
<div id="question_block">
testing form
</div>
<a href="javascript: submit();">Submit</a>
</form>
<script type='text/javascript'>
function submit()
{
document.forms["myform"].submit();
}
</script>
you can check it out here : http://jsfiddle.net/Zm426/7/
Upvotes: 4
Reputation: 176896
Submit your form like this ...
Your HTML code
<form name="myform" action="handle-data.php"> Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
</form>
JavaScript Code
<script type="text/javascript">
function submitform() { document.myform.submit(); }
</script>
Upvotes: 6