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.

7
  • 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? Commented Sep 19, 2011 at 5:06
  • Sounds like looking for ajax tutorial. have a look at my answer here stackoverflow.com/questions/7199797/some-ajax-help/… Commented Sep 19, 2011 at 5: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. Commented Sep 19, 2011 at 5:25
  • :if you think i helped you.please upvote that answer stackoverflow.com/questions/7199797/some-ajax-help/… SO not wish to place duplicate answer.Thanks for your appreciation Commented Sep 19, 2011 at 5:31
  • Will be happy to. Can you please answer the question from my previous comment? Commented Sep 19, 2011 at 5:39

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 <form> look like this:- <form action="otherpage.php" method="POST"> ... ... </form> . plain simple html way to doing things!
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.
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
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.
|
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>

6 Comments

What does console.log do? What do you mean "prints yahoo"? Where did 'yahoo' come from?
Thanks, but I want to use POST, not GET. So, your original answer wont redirect to the new page?
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.
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.
|
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
    }
});

4 Comments

In my case server.php and window.href are the same. Isn't this like calling for the same page twice?
Also, I don't need a reply from the php page. I just want to redirect to it and pass it parameters.
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, thanks for your generous help. It wasn't exactly what I was looking for but you get +1 on the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.