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;
});
Share:
15,577
Tin
Author by

Tin

Updated on June 04, 2022

Comments

  • Tin
    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 2 hrefs below. Furthermore, I would like also to get rid of the submit 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
    Lenin over 11 years
    See the changes I have made. I am not giving full code, just your homework.
  • Lenin
    Lenin over 11 years
    You can even run this using the same form but with condition on the action script.
  • Jivings
    Jivings over 11 years
    Although 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
    Tin over 11 years
    in 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 3 forms, one outer, which contains these 2 new forms, is it possible to have nested forms?
  • Lenin
    Lenin over 11 years
    You do not need nested forms or you do not need more than one if you can write the Action Scripts conditions properly.
  • Jamie Taylor
    Jamie Taylor over 11 years
    True, but to map anchor links to post requests, it'll inevitably end up at ajax at some point.
  • Jivings
    Jivings over 11 years
    No anchors. They should be form elements.
  • Jamie Taylor
    Jamie Taylor over 11 years
    I'm just going by what was posted in the question, mapping <a> to Post
  • Jivings
    Jivings over 11 years
    Stack 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
    Lenin over 11 years
    see this post for form nesting
  • Tin
    Tin over 11 years
    do you have a link for proper action script condition?
  • Lenin
    Lenin over 11 years
    Check my post with the change I've made. Save this in a php file and press the three buttons. You'll see the changes.