-1

i have this (simplified)

<script>

  dataToSend = 'dummy data here';

function sendIt() {

//code here

}
</script>

<a href='#' onclick='sendIt();'>Click here to send data</a>

what would i need to put in sendIt() to do a POST submit (not ajax, i want the user to be sent to the page too).

using jquery

slycat
  • 670
  • 3
  • 13
  • 29

4 Answers4

1

I don't know what you would want to with this but here you go

<script type="text/javascript">
dataToSend = 'dummy data here';
function sendIt() {
        $('body').append('<form id="sendit" method="post"><input type="hidden" name="data" value="'+dataToSend+'"/></form>');
        $('#sendit').submit();
}
</script>

<a href='#' onclick='sendIt();'>Click here to send data</a>
Musa
  • 89,286
  • 16
  • 105
  • 123
0

Here's a work around. Why not create a form and a hidden text field that contains the string, and trigger the form submit when the link is clicked.

<form id="fauxForm" method="POST">
  <input type="hidden" value="dummy data here" />
</form>
<a id="sendData" href="submithere.php">Click here to send data</a>

<script type="text/javascript">
  $("#sendData").click(function(ev){
    ev.preventDefault();
    $("#fauxForm").attr("action",$(this).attr("href")).submit();
  });
</script>
xar
  • 1,349
  • 2
  • 16
  • 28
0

The easy way is to add a form. You do not need javascript.

<form action=" .. url.." method="post">
    <input type="hidden" name="dataToSend" value="dummy data here">
    <input name="sendIt" id="sendIt" type="submit" value="Click here to send data">
</form>
voscausa
  • 9,982
  • 2
  • 29
  • 55
-1

Short answer is it isn't possible to send a post from JavaScript alone. Longer answer is available from pass post data with window.location.href On the other hand, you can turn it into a GET request (e.g. http://www.misyte.com/page?param=value&param=value&etc=data) and get the data to the page that way.

Community
  • 1
  • 1
robrich
  • 12,467
  • 5
  • 30
  • 57