I would like from my href, it went to the "spincoin.php" page with the post method, sending the variable "1", how can I do that?
Code:
<div>
<a href="spincoin.php">
<h2>Product</h2>
<p>Cost: 100P</p>
</a>
</div>
I would like from my href, it went to the "spincoin.php" page with the post method, sending the variable "1", how can I do that?
Code:
<div>
<a href="spincoin.php">
<h2>Product</h2>
<p>Cost: 100P</p>
</a>
</div>
I find impossible post method by url. What you can do is disguise the button.
As we do not want the form to be viewed, we just want to send its data by POST, the form field is hidden, that is, hidden.
1: Button as if it were an image
The value to be passed is in the input hidden 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="https://pt.stackoverflow.com/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="https://pt.stackoverflow.com/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 will better serve your HTML
As @AndersonCarlosWoss commented, it is not possible to send a <a>
element to use POST
method directly. To make POST
, you need a <form>
element:
<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 <form>
element represents a form, which consists of a series of fields, which are represented by the elements <input>
, <select>
, <textarea>
and <button>
descendants of the form.
The <form>
has a method
attribute that tells which HTTP method to use; in this case, you want to use POST
, so that's what we put. The action
attribute indicates which URL the form should be submitted to.
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 fields visible: text ( <input type="text">
), dropdown ( <select>
), checkbox ( <input type="check">
), buttons <button>
or <input type="button">
). When the form is submitted, it collects all its descendants that are fields and sends them to the destination URL with either the id
attribute or the name
attribute as the key (if both are set, id
has preference) and the value
attribute as value.
Finally, the form is usually sent by clicking on a <input type="submit">
control, but with a little javascript we can make a common link > schema of URL javascript:
. Just get a reference to form
(in the example, using document.querySelector()
and call the submit()
method, the rest works automatically.)
One last caveat : If you want to transfer an attachment (via the <input type="file">
control), <form>
will need to have the enctype="multipart/form-data"
attribute default is application/x-www-form-urlencoded
). Otherwise, the attachment will not be sent.
Look also we can 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; ?>
Looking to read about the .post of jquery it is very useful.