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>
You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.
The <button> tag with a type="submit" attribute creates a submit button. When clicked, submit buttons send form data to a form handler.
The <input type="submit"> defines a submit button which submits all form values to a form-handler. The form-handler is typically a server page with a script for processing the input data. The form-handler is specified in the form's action attribute.
The Submit Button The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With