简体   繁体   中英

Submit a form using a link and pass a post variable

Hi I want to use an anchor tag to submit a form. However the only problem is that my form had an input button (now replaced by anchor tag) that passed a post variable so that in my php script I could check to see if that post variable was set. How can I achieve this same technique using an achor tag? Here is what I have tried:

FORM USING AN INPUT BUTTON (old):

<form method="post" action="delete.php" onsubmit="return confirm('Do you really want to delete?');">
<input class="delbtn" name="deleteuser" type="submit" value="Delete"/>
</form>

FORM USING ANCHOR TAG INSTEAD OF INPUT SUBMIT (new):

<form id="delform" method="post" action="delete.php" onsubmit="return confirm('Do you really want to delete?');">
<a href="javascript:void();" onclick="document.getElementById('delform').onsubmit();" class="delbtn" >Delete</a>
</form>

In my php class I am checking to see if the form was submitted using the post variable from the input button ("deleteuser"). However how can I do the same using an anchor tag?

You've almost got it in your second try, just call .submit() instead of onsubmit().

<form id="delform" method="post" action="delete.php" onsubmit="if( confirm('Do you really want to delete?')) {this.submit();}">
<a href="javascript:void();" onclick="document.getElementById('delform').onsubmit();" class="delbtn" >Delete</a>
<input type="hidden" name="action" value="deleteuser" />
</form>

Here is a JSFiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM