1

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.

Ash
  • 1,239
  • 2
  • 17
  • 24
  • Well, I know how to use ajax to run code on a php page but I don't know how to open a php page and pass it parameters. Can you give an example? – Ash Sep 19 '11 at 05:06
  • Sounds like looking for ajax tutorial. have a look at my answer here http://stackoverflow.com/questions/7199797/some-ajax-help/7199821#7199821 – Gowri Sep 19 '11 at 05:11
  • OK, I think I got it. Now, what if I don't want an answer from the PHP page, but I want open and stay on it? I wish you wrote an answer instead of a comment so I can give you the credit you deserve. – Ash Sep 19 '11 at 05:25
  • :if you think i helped you.please upvote that answer http://stackoverflow.com/questions/7199797/some-ajax-help/7199821#7199821.because SO not wish to place duplicate answer.Thanks for your appreciation – Gowri Sep 19 '11 at 05:31
  • Will be happy to. Can you please answer the question from my previous comment? – Ash Sep 19 '11 at 05:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3579/discussion-between-gowri-and-ash) – Gowri Sep 19 '11 at 05:44
  • gowri, I just saw your invitation for chat. I'm there if you can still chat. Thanks. – Ash Sep 19 '11 at 05:55

3 Answers3

3
$.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

Community
  • 1
  • 1
Jags
  • 1,448
  • 1
  • 17
  • 32
  • And this will cause otherpage.php to show? Also, I didn't understand your note on the data line. I want to pass parameters from the current page to the one I'm calling and show it. – Ash Sep 19 '11 at 05:35
  • I thought you were looking for an ajax way to open that other page.. anyways, in you case you just need to make sure your
    look like this:-
    ... ...
    . plain simple html way to doing things!
    – Jags Sep 19 '11 at 06:00
  • You understood correctly. I am looking for an ajax way to open that other page, but I also want to pass it parameters. The `form` option is not very good since I have several buttons and each will open the same page but with different parameters. – Ash Sep 19 '11 at 06:09
  • updated again..postToUrl will POST your custom parameters to the new page..and since this is not ajax..your otherpage.php should open in browser – Jags Sep 19 '11 at 10:18
  • JamesSmith, thanks so much for this detailed response. You understood what I was looking for and I bet it would work so I'm accepting your answer. But this is getting too complicated for me and I realize now that my strategy was wrong and I should have done more work with PHP rather than JS, then I wouldn't have been in this situation. So I'm going to solve this by writing the params to the DB, redirect to the page and read them again. I know it's bad programming but this is just temporary. Right now I just have to finish building my site as fast as possible. – Ash Sep 21 '11 at 18:08
  • yea..sometimes..whatever-you-can-code-faster is the best way to go :) – Jags Sep 21 '11 at 18:36
2

you can use query string parameters

$.post("anotherPage.php?param1=one&param2=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&param2=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>
Rafay
  • 30,236
  • 5
  • 64
  • 98
  • What does `console.log` do? What do you mean "prints yahoo"? Where did 'yahoo' come from? – Ash Sep 19 '11 at 05:37
  • Thanks, but I want to use POST, not GET. So, your original answer wont redirect to the new page? – Ash Sep 19 '11 at 05:46
  • no the original answer wont redirect to the page2 also in your scenario as far as i have understood you cannot use `POST` because the query string parameters are visible in the address bar so you will have to use `GET`. – Rafay Sep 19 '11 at 05:51
  • Is there a way for the parameters not to be visible in the address bar? This is why I want to use POST and not GET if possible. – Ash Sep 19 '11 at 05:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3580/discussion-between-3nigma-and-ash) – Rafay Sep 19 '11 at 05:56
  • 3nigma, thanks a lot for the effort. It wasn't exactly what I was looking for but it was detailed and I'm sure others can benefit from it. So +1 on the answer. – Ash Sep 21 '11 at 18:13
1

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
    }
});
Paweł Tomkiel
  • 1,848
  • 2
  • 19
  • 38
Gowri
  • 15,559
  • 25
  • 95
  • 154
  • In my case `server.php` and `window.href` are the same. Isn't this like calling for the same page twice? – Ash Sep 19 '11 at 06:13
  • Also, I don't need a reply from the php page. I just want to redirect to it and pass it parameters. – Ash Sep 19 '11 at 06:14
  • you can't do that in server page .you can only do this in client page.I think php header location will not working in server page. – Gowri Sep 19 '11 at 06:24
  • gowri, thanks for your generous help. It wasn't exactly what I was looking for but you get +1 on the answer. – Ash Sep 21 '11 at 18:10