2

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>
Mohammed H
  • 6,326
  • 12
  • 72
  • 119
Tin
  • 966
  • 1
  • 14
  • 27
  • 6
    You can't submit POST without either AJAX or a form. – Jivings Dec 11 '12 at 08:37
  • @Jivings, i don't want to get rid of the `form`, but of passing the variables in the URL – Tin Dec 11 '12 at 08:39
  • You just need to replace your links with input elements. – Jivings Dec 11 '12 at 08:42
  • @Jivings, but which shall be the `value` for a`hidden` input element, for instance, i've `action=delete` or `action=add` which appear into 2 separate hyperlinks. – Tin Dec 11 '12 at 08:46
  • The name of the element is `action` and the value is `delete` or `add`. Have a read up on HTML Forms. – Jivings Dec 11 '12 at 08:49

3 Answers3

2

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;
});
Jamie Taylor
  • 4,534
  • 5
  • 40
  • 60
  • 1
    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. – Jivings Dec 11 '12 at 08:50
  • 1
    True, but to map anchor links to post requests, it'll inevitably end up at ajax at some point. – Jamie Taylor Dec 11 '12 at 08:59
  • No anchors. They should be form elements. – Jivings Dec 11 '12 at 09:03
  • I'm just going by what was posted in the question, mapping to Post – Jamie Taylor Dec 11 '12 at 09:09
  • 1
    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](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Jivings Dec 11 '12 at 09:16
0

You can keep the input tags as hidden in your form to submit them as POST.

<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>

Rewrite the above using two forms, having <input type="hidden" name="id /> and buttons <input type="button" name="delete" /> etc..

if you run print_r($_POST) in your action script. You'll get a list of the buttons and the values of the hidden fields. Now, you can write conditions for the actions to run. I didn't write complete code here, just passing the idea.

Edit: See the example below.

     <form  method="post" id="cart">
        <table>
                <input type="hidden" name="id" value='some value' />
                <input type="submit" name="action" value="delete" />
                <input type="submit" name="action" value="add" />
            </tr>
            <tr>
                ...
            </tr>
        </table>
        <div><button type="submit">Update</button></div>
     </form>


    <?php print_r($_POST); ?>
Lenin
  • 580
  • 16
  • 34
  • See the changes I have made. I am not giving full code, just your homework. – Lenin Dec 11 '12 at 08:47
  • You can even run this using the same form but with condition on the action script. – Lenin Dec 11 '12 at 08:48
  • in case, I use the two additional forms, one having ``, the other ``, 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 `form`s? – Tin Dec 11 '12 at 08:54
  • You do not need nested forms or you do not need more than one if you can write the Action Scripts conditions properly. – Lenin Dec 11 '12 at 08:58
  • see this post for [form nesting](http://stackoverflow.com/questions/379610/can-you-nest-html-forms) – Lenin Dec 11 '12 at 09:49
  • do you have a link for proper action script condition? – Tin Dec 11 '12 at 10:06
  • 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. – Lenin Dec 11 '12 at 10:32
0

As far as i know you can't perform any POST requests with links. You can make your GET requests with links and php as a fallback for users with javascript disabled and for those who have javascript enabled cancel default behavior for links with javascript and make with ajax your POST request with help of AJAX.

for example:

<a class="submit" href="hallo.html?hello=world&test=yes">Test</a>

and js(i used jquery) would be:

$('.submit').click(function(){
    var urlPairs = this.href.split('?')[1].split('&'),
        total = urlPairs.length,
        current = [],
        data = {};

    for(;total;) {
        current = urlPairs[--total].split('=');
        data[current[0]] = current[1];
    }

    $.post('test.php', data);

    return false;
});

you could also write your POST function other for more info check jQuery post function doc

P.S. I wounder, if you are using FORM any way why wouldn't you use submit button, is it because of CSS styling? You could style it the same way like any other element(with some tweaks in some browsers).

orustammanapov
  • 1,598
  • 4
  • 24
  • 38