Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit form using <a> tag

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> 
like image 424
Potheek Avatar asked Apr 06 '12 06:04

Potheek


People also ask

How do I submit a form with a tag?

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.

Can a tag be type submit?

The <button> tag with a type="submit" attribute creates a submit button. When clicked, submit buttons send form data to a form handler.

Is there a submit tag in HTML?

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.

How do you submit a form in HTML?

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.


2 Answers

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.

like image 80
krystan honour Avatar answered Sep 22 '22 04:09

krystan honour


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> 
like image 22
Hemant Nagpal Avatar answered Sep 25 '22 04:09

Hemant Nagpal