Question

I need to load a new page into the browser. I've used "window.location=somePage.html?arg1=val1&arg2=val2..." and so on.

But I must load the page into the browser without the 'GET' params being shown in the URL bar when the new page loads.

In other words, I need to load the new page -- and pass to it arg1=val1 and arg2=val2 etc. but NOT have the arg1, arg2 etc. name-value pairs appear in the URL bar.

I suspect I could tinker around with mod_rewrite rules. DO NOT WANT TO DO THAT.

What I'd like to do is somehow pass 'POST' parameters -- like a form does -- to the server and have a new page load into the browser and the new page's PHP code somehow receives the arg1, and arg2 parameters so the new page's PHP code can use them.

Not sure how to do this. Ajax is the wrong answer, because while I can use Ajax to pass the arg1, arg2, etc. to the server, the whole point of Ajax is to avoid having to load a new page.

What I need is:

   // THE ARGS SYNTAX ON THE RIGHT HERE IS *COMPLETELY* MADE UP.
  window.location "http://mysite/theNewPage<<arg1="val1" + <<arg2="val2"

How can I load a new page, pass name/value pairs to the new page WITHOUT them appearing in the URL bar?

NOT USING JQUERY at all here.

Was it helpful?

Solution 2

Seems to me that you need session variables. Start your session in every page that you gonna need your variables. session_start();

Set your variables

$_SESSION['arg1'] = 'some value';

$_SESSION['arg2'] = 'some other value';

Then use it on other page.

echo $_SESSION['arg1'];

OTHER TIPS

It's not possible to send a POST request using window.location.href. What you have to do is to set up a form tag with data fields in it, set the action attribute of the form to the URL and the method attribute to POST, then call the submit method on the form tag.

<form id="myform" action="http://mysite/theNewPage.php" method="post">
   <input type="hidden" name="arg1" value="val1">
   <input type="hidden" name="arg2" value="val2">       
</form>

Then you can call the following in javascript:

document.getElementById('myform').submit();

Retrieve the contents in the other script using: $arg1 = $_POST['arg1']; and $arg2 = $_POST['arg2'];.

Good luck!

Try this...

  1. First set the cookie in the browser using javascript.

  2. Execute this javascript code.

       window.location "http://mysite/theNewPage
    

    Hence you will redirect to "http://mysite/theNewPage

  3. Check the parameters set in the cookie before render in PHP file.

  4. In the PHP file you will get the set cookie parameters. According display the page content.

  5. To set cookie using javascript use this code

    // sets the cookie cookie1 document.cookie = 'cookie1=test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

In this you are going to set parameters using cookie which are accessible in server.

Manually create a form to the URL and using the POST method and submit the form. Here is solution both in pure JavaScript and JQuery-

pure-JavaScript solution-

function post_redirect(URL, args){
    var form_tag = document.createElement("form");
    form_tag.setAttribute('method',"POST");
    form_tag.setAttribute('action', URL);
    for(key in args){
        var input_tag = document.createElement("input");
        input_tag.type = "text";
        input_tag.name = key;
        input_tag.value = args[key];
        form_tag.appendChild(input_tag);
    }
    form_tag.submit();
}

Using JQuery -

function post_redirect(URL, args){
    form_str = '<form method = "POST" action = ' + URL + '>';
    for(key in args){
        form_str += '<input type="text" name="'+ key +'" value = "'+ args[key] +'"/>';
    }
    $(form_str).submit();
}

How it works-

args = {
            "name1" :   "value1",
            "name2" :   "value2",
            "name3" :   "value3",
            "name4" :   "value4"
        };
URL = "a.php";
post_redirect(URL, args);
//Re-directed to page pointed by URL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top