How to send a HREF with POST method or disguise?

Asked

Viewed 11,315 times

3

I would like from my href, it go to the page "spincoin.php" with the post method, sending the variable "1", as I can do this?

Code:

<div>
    <a href="spincoin.php">
        <h2>Product</h2>
        <p>Cost: 100P</p>
    </a>
</div>
  • Why not do this using the element form even?

  • I am a little layman in HTML and I did it the way I thought right, I don’t know how I would look with the form, since it only accepts inputs (I think). If you can do with the form, as it would be?

  • You can fool the user, make him think it’s a link

  • edit the question I have a ready example

  • How do I do that? Can you send me an example? @Leocaracciolo

  • edit the question, How to send a href with POST method, and if it is not possible, at least disguise

  • but in your example gets a little complicated the link embraces a lot, img, H2, p, p

  • @Leocaracciolo in this way?

  • had forgotten the post method

  • Here shows with doing using Javascript http://blog.rdtecnologia.com.br/php/passando-dados-via-post-em-javascript/ Greetings, Itamar M. Lins Jr.

Show 5 more comments

3 answers

4

As @Andersoncarloswoss commented, it is not possible to send an element <a> method POST directly. To make POST, you need an element <form>:

<form method="POST" action="spincoin.php">
    <input type="hidden" name="1" value="1">
    <a href="javascript:document.querySelector('form').submit();">Clique aqui</a>
</form>

The element <form> represents a form, consisting of a series of fields, which are represented by the elements <input>, <select>, <textarea> and <button> descended from the form.

The <form> has an attribute method which tells you which HTTP method to use; in this case, you want to use POST, then that’s what we put. The attribute action indicates to which URL the form should be sent.

In this case, our form only has a hidden field (that is, it does not appear for the user) whose name is 1 and whose value is also 1. There are other types of visible fields: text (<input type="text">), dropdown (<select>), checkbox (<input type="check">), buttons (<button> or <input type="button">). When the form is sent, it collects all its descendants that are fields and sends them to the destination URL with or attribute id or the attribute name as key (if both are defined, id has preference) and the attribute value as a value.

Finally, usually the form is sent by clicking on a control <input type="submit">, but with a little bit of javascript we can make a link common send it, using the schema url javascript:. Just get a reference to form (in the example, using document.querySelector() and call the method submit(). The rest works automatically.

One last caveat: if you want to transfer an attachment file (through control <input type="file">), the <form> going need have the attribute enctype="multipart/form-data" (the standard is application/x-www-form-urlencoded). Otherwise the attachment will not be sent.

  • I think if you use this.parentNode.submit() in the element a is better not only in terms of performance, because the JS does not need to search for the element, as you allow the existence of multiple forms on the page.

  • @Andersoncarloswoss Of course, there are several methods of obtaining a reference to the form; I chose one at random. this.parentNode may be an option, but I did not test to see if it worked correctly or not. Anyway, the idea was to explain to OP what is an element <form> and what its characteristics are so that it chooses what is good for it and what is not good for it, instead of just copying and pasting a ready answer.

2


I find it impossible to post method by url. What you can do is disguise the button.

Since we don’t want you to see the form, we just want to send your data by POST, the form field is Hidden, ie hidden.

1: Button as if it were an image

The value to be passed is in the Hidden input of name var and value 1

.tim{
border: 2px solid #AD235E;
border-radius: 100px;
width: 150px;
height: 150px; background-image: url(http://kithomepage.com/sos/kkl.png);
}
.tim:hover {
    cursor: pointer;
}
<form action="/questions?sort=newest" method="POST">
    <input type="hidden" name="var" value="1" />
    <button class="tim">
    </button>
</form>

2: Button as if it were a link

.tim {
    border: 0;
    padding: 0;
    display: inline;
    background: none;
    text-decoration: underline;
    color: blue;
}
button:hover {
    cursor: pointer;
}
<form action="/questions?sort=newest" method="POST">
    <input type="hidden" name="var" value="1" />
    <button class="tim">Pagina destino
    </button>
</form>

If the information to be passed is not relevant you can pass the GET method using a parameter in the URL itself <a href="spincoin.php?var=1"....

<div style="height:200px;width:350px;position:static;margin-left:120px;border-style:solid">
<a href="spincoin.php?var=1" style="border-bottom: none;">
    <img style="width: 360px;" src="images/btn.png"/>
    <h2 style="color:white;font-size:24px;position:absolule;margin-top:-180px;margin-left:15px;">FREE HACK - Wallhack</h2>
    <p style="margin-left:15px;">See all through the wall with the free hack.</p>
    <p style="margin-right:10px; text-align: right; margin-top:53px; font-size: 25px; color: white">Cost: 100P</p>
  </a>
 /div>

and retrieve on the destination page as follows:

   $var = $_GET["var"];

This way will suit your HTML better

  • had forgotten the post method

  • Thanks for the tip, I’ll test the get method.

2

Look we can also use Jquery to make a post, example:

<?php
 // trecho q devera gerar a saida
 // coloquei .PHP_EOL caso queira interromper a execucao e depurar erros
 $jquery = ''.PHP_EOL; 
 $jquery .= ' var MinhaVarAqui1 = "'.$var1.'";'.PHP_EOL;
 $jquery .= ' var MinhaVarAqui2 = "'.$var1.'";'.PHP_EOL;
 $jquery .= ' var MinhaVarAqui3 = "'.$var1.'";'.PHP_EOL;

 // va declarando as variaveis a serem postadas conforme a necessidade

 // aqui vamos realizar o post sem form
 $.post("INFORME_A_URL_DESEJADA_AQUI ex: http://meusite.com/scripa.php",{ var1:MinhaVarAqui1, var2:MinhaVarAqui2, var3:MinhaVarAqui3 });

 $jquery .= ''.PHP_EOL;
?>

Seeks to read about the . jquery post it is very useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.