javascript - Replacing GET with POST - Stack Overflow

Here I am trying to redirect new page and I want to send a variable. It uses GET.How can I do it with P

Here I am trying to redirect new page and I want to send a variable. It uses GET.

How can I do it with POST?

window.location.href = 'start.php?userid='+userid;

start.php

<?php
    $user_id=$_GET['userid']; //should be post
?>

Here I am trying to redirect new page and I want to send a variable. It uses GET.

How can I do it with POST?

window.location.href = 'start.php?userid='+userid;

start.php

<?php
    $user_id=$_GET['userid']; //should be post
?>
Share Improve this question edited Nov 3, 2013 at 12:28 Noctis 11.8k3 gold badges45 silver badges84 bronze badges asked Nov 3, 2013 at 12:13 user123user123 5,40516 gold badges78 silver badges125 bronze badges 3
  • 2 Why would you need to use a POST? – moonwave99 Commented Nov 3, 2013 at 12:23
  • Also note that you need to use encodeURIComponent when passing variable names and values manually. – Marcel Korpel Commented Nov 3, 2013 at 12:27
  • @moonwave99: it's prone to be hacked – user123 Commented Nov 4, 2013 at 4:10
Add a ment  | 

5 Answers 5

Reset to default 3

You will have to submit the data to the server, not just redirect the browser.

In your case, as it looks like you want full page refresh anyway just create a form on the fly:

var oForm = document.createElement("form");
oForm.method = "POST";
oForm.action = "start.php";
var oInput = document.createElement("input");
oInput.name = "userid";
oInput.value = userid;
oForm.appendChild(oInput);
document.body.appendChild(oForm);
oForm.submit();

You need to declare a fake form in HTML and a link that will trigger javascript to submit the form, like below

<form action="start.php" method="post">
<input type="hidden" name="userid" value="[userid]">
</form>

<script type="text/javascript">
function submitlink() 
{
document.forms[0].submit();
}
</script>
<a class="buttondown" onclick="submitlink()">Submit Link</a>

I think this answer may help you:
How do you force a web browser to use POST when getting a url?

You just need to create a form on demand using javascript to send data with POST method when clicking a link.
So basically it's just this part:

<script type="text/javascript">
function submitAsPost(url) {
    var postForm = document.createElement('form');
    postForm.action = url;
    postForm.method = 'post';
    var bodyTag = document.getElementsByTagName('body')[0];
    bodyTag.appendChild(postForm);
    postForm.submit();
}
</script>
<a href="/my/url" onclick="submitAsPost(this.href); return false;">this is my post link</a>

I was in the same problem you had. If you are using jquery.redirect.min.js as your jquery plugin, You can use as below. It gives you POST method.

   $().redirect("myPhp.php", { name: "John" });

All you need is to download jquery.redirect.min.js file from here and link it with your php file, and use as above. That's it. Hope I helped.

Works fine for me.

You can post form by html tag and . Otherwise, see "jQuery.post" for async post.
For the 1st case you create FORM tag, put INPUT type="hidden" inside and set its value that will be posted.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745036474a4607548.html

相关推荐

  • javascript - Replacing GET with POST - Stack Overflow

    Here I am trying to redirect new page and I want to send a variable. It uses GET.How can I do it with P

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信