将参数从一个页面上的 JS/Jquery 传递到另一页面上的 PHP

发布于 2024-12-05 04:05:11 字数 146 浏览 1 评论 0原文

我正在寻找使用 POST 方法将参数从一个页面上的 javascript/jquery 传递到另一个页面上的 PHP 的最简单方法。

我应该提到这两个页面都是 WordPress 页面的 PHP 模板,但我想这应该不重要。

希望双方提供代码示例。

I'm looking for the simplest way to use the POST method for passing parameters from javascript/jquery on one page to PHP on another page.

I should mention that both pages are PHP templates for wordpress pages, but I guess it shouldn't matter.

Would appreciate examples for code on both sides.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏见 2024-12-12 04:05:11
$.ajax({
  type: 'POST',
  url: 'otherpage.php',
  data: $('form').serialize(), //you may want to give id of the form which contains elements you want to POST
  success: function(){
      //code on success
  },
  dataType: 'html'
});

编辑1
如果您想指定要发送的自己的参数,请使用:

$.ajax({
  type: 'POST',
  url: 'otherpage.php',
  data: {
          paramname1 : "put_param_value_here",
          paramname2 : "put_param_value_here",
          paramname3 : "put_param_value_here"
        },
  success: function(){
      //code on success
  },
  dataType: 'html'
});

编辑2:如果您只想将自己的参数发布到页面(无ajax。简单的P​​OST将打开页面,则执行以下操作)

function postToUrl(url, params, newWindow)
{
    var form = $('<form>');
    form.attr('action', url);
    form.attr('method', 'POST');
    if(newWindow){ form.attr('target', '_blank'); }

    var addParam = function(paramName, paramValue){
        var input = $('<input type="hidden">');
        input.attr({ 'id':     paramName,
                     'name':   paramName,
                     'value':  paramValue });
        form.append(input);
    };

    // Params is an Array.
    if(params instanceof Array){
        for(var i=0; i<params.length; i++){
            addParam(i, params[i]);
        }
    }

    // Params is an Associative array or Object.
    if(params instanceof Object){
        for(var key in params){
            addParam(key, params[key]);
        }
    }

    // Submit the form, then remove it from the page
    form.appendTo(document.body);
    form.submit();
}

像这样调用:-

postToUrl('otherpage.php', {paramname1: "value_here", paramname2: "value_here"});

代码取自 JavaScript post 请求(如表单提交)的答案

$.ajax({
  type: 'POST',
  url: 'otherpage.php',
  data: $('form').serialize(), //you may want to give id of the form which contains elements you want to POST
  success: function(){
      //code on success
  },
  dataType: 'html'
});

edit 1
incase you want to specify your own parameters to send then use:

$.ajax({
  type: 'POST',
  url: 'otherpage.php',
  data: {
          paramname1 : "put_param_value_here",
          paramname2 : "put_param_value_here",
          paramname3 : "put_param_value_here"
        },
  success: function(){
      //code on success
  },
  dataType: 'html'
});

Edit 2: if you only want to post your own parameters to a page (no ajax. simple POST which will open the page then do the following)

function postToUrl(url, params, newWindow)
{
    var form = $('<form>');
    form.attr('action', url);
    form.attr('method', 'POST');
    if(newWindow){ form.attr('target', '_blank'); }

    var addParam = function(paramName, paramValue){
        var input = $('<input type="hidden">');
        input.attr({ 'id':     paramName,
                     'name':   paramName,
                     'value':  paramValue });
        form.append(input);
    };

    // Params is an Array.
    if(params instanceof Array){
        for(var i=0; i<params.length; i++){
            addParam(i, params[i]);
        }
    }

    // Params is an Associative array or Object.
    if(params instanceof Object){
        for(var key in params){
            addParam(key, params[key]);
        }
    }

    // Submit the form, then remove it from the page
    form.appendTo(document.body);
    form.submit();
}

call like this:-

postToUrl('otherpage.php', {paramname1: "value_here", paramname2: "value_here"});

code taken from answer on JavaScript post request like a form submit

我还不会笑 2024-12-12 04:05:11

您可以使用查询字符串参数

$.post("anotherPage.php?param1=one¶m2=two",function(data){ 
console.log("data"); //prints yahoo
console.log("success");    
});

$.post("anotherPage.php",{param1:'one',param2:'two'},function(data){ 
console.log("data"); //prints yahoo
console.log("success");    
});

在 php 页面中

$param1=$_POST['param1'];//gives you one
$param2=$_POST['param1'];//gives you two

echo "yahoo";

编辑

从评论中没有 JamesSmith 答案我认为您想在这种情况下重定向到其他页面

Page1.php

<script>
location.href='page2.php?param1=one¶m2=two'; // this causes the page to redirect
</script>

page2.php

<h1><?php echo $_GET['param1']; ?></h1>
<h2><?php echo $_GET['param2']; ?></h2>

另一个编辑

您可以在 page1 中有一个表单并将其发布到

<form action="page2.php" method="post">
<input name="param1" type="text" />
<input name="param2" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>

page2.php 中的page2

<h1><?php echo $_POST['param1']; ?></h1>
<h2><?php echo $_POST['param2']; ?></h2>

you can use query string parameters

$.post("anotherPage.php?param1=one¶m2=two",function(data){ 
console.log("data"); //prints yahoo
console.log("success");    
});

or

$.post("anotherPage.php",{param1:'one',param2:'two'},function(data){ 
console.log("data"); //prints yahoo
console.log("success");    
});

in the php page

$param1=$_POST['param1'];//gives you one
$param2=$_POST['param1'];//gives you two

echo "yahoo";

EDIT

from the comment no JamesSmith answer i think you want to redirect to other page in that case

Page1.php

<script>
location.href='page2.php?param1=one¶m2=two'; // this causes the page to redirect
</script>

page2.php

<h1><?php echo $_GET['param1']; ?></h1>
<h2><?php echo $_GET['param2']; ?></h2>

yet another edit

you can have a form in page1 and post it to page2

<form action="page2.php" method="post">
<input name="param1" type="text" />
<input name="param2" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>

in page2.php

<h1><?php echo $_POST['param1']; ?></h1>
<h2><?php echo $_POST['param2']; ?></h2>
何以畏孤独 2024-12-12 04:05:11

你可以根据响应在ajax成功中做你想做的事情

$.post('server.php', ({parm:"1"}) function(data) {
    if (data == 1){
        window.href = "your redirecting location";
    }
    else
    {
        //do something
    }
});

you can do what you wish to do in ajax success according to response

$.post('server.php', ({parm:"1"}) function(data) {
    if (data == 1){
        window.href = "your redirecting location";
    }
    else
    {
        //do something
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文