href hyperlinks as POST
15,577
I'd suggest using jQuery.post and click. When the link is clicked, submit the data via something like this
$('.r').on('click', function() {
$.post('test.php', {key:'value', key2:'value2'}, function(data) {
//error check here
});
return false;
});
Author by
Tin
Updated on June 04, 2022Comments
-
Tin almost 2 years
I'm wondering which is the best way to map
<a href=...></a>
hyperlinks performing a HTTP GET to perform a HTTP POST (I'd like to avoid passing all variables in the URL)? For instance, the 2hrefs
below. Furthermore, I would like also to get rid of thesubmit
button and use a regular<a href=...></a>
hyperlink. Any suggestions?<form action="test.php?action=update" method="post" id="cart"> <table> <tr> <td> <a href="test.php?action=delete&id=<?php echo $id ?>" class="r"> remove </a> </td> <td> <a href="test.php?action=add&id=<?php echo $id ?>" class="r">add</a> </td> </tr> <tr> ... </tr> </table> <div> <button type="submit">Update</button> </div> </form>
-
Lenin over 11 yearsSee the changes I have made. I am not giving full code, just your homework.
-
Lenin over 11 yearsYou can even run this using the same form but with condition on the action script.
-
Jivings over 11 yearsAlthough this is correct, it's more important for OP to learn how to correctly use HTML forms before they start using jQuery and making AJAX calls.
-
Tin over 11 yearsin case, I use the two additional forms, one having
<input type="hidden" name="action" value="add"/>
, the other<input type="hidden" name="action" value="add"/>
, I'll have at the end of the day 3forms
, one outer, which contains these 2 new forms, is it possible to have nestedform
s? -
Lenin over 11 yearsYou do not need nested forms or you do not need more than one if you can write the Action Scripts conditions properly.
-
Jamie Taylor over 11 yearsTrue, but to map anchor links to post requests, it'll inevitably end up at ajax at some point.
-
Jivings over 11 yearsNo anchors. They should be form elements.
-
Jamie Taylor over 11 yearsI'm just going by what was posted in the question, mapping <a> to Post
-
Jivings over 11 yearsStack Exchange is not just about answering questions, it's about making sure the poster goes away with a better understanding. This is an XY problem.
-
Lenin over 11 yearssee this post for form nesting
-
Tin over 11 yearsdo you have a link for proper action script condition?
-
Lenin over 11 yearsCheck my post with the change I've made. Save this in a php file and press the three buttons. You'll see the changes.