Necesita publicar en el servidor para cargar una nueva página

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 y 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 y 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 evitar having to load a new page.

Lo que necesito es:

   // 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.

preguntado el 08 de febrero de 14 a las 17:02

Why can't you just use a normal form, without script at all? -

I'm working with some legacy code and were I to rewrite it that would be an option -- right now, I am chartered with converting anchor tags to a function call that can pass params to the "src" that was in the anchor tags. Lots of them. It would require re-architecting more code so for expediency I cannot. -

4 Respuestas

It's not possible to send a POST solicitud usando 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']; y $arg2 = $_POST['arg2'];.

¡Buena suerte!

Respondido 08 Feb 14, 17:02

+1 correct answer ... and hey i cant delete answer alone i am just 20k user not mode :) - NullPoièteя

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'];

Respondido 08 Feb 14, 17:02

yep thought of that. I'm kind of new to all this and was hoping there was an analog for the very-easy-to-use "window.location=mynewpage.html?arg1=val1&arg2=val2", I posted here hoping that despite my inability to find it on SO or elsewhere, an easy "window.location=" type of way to do this. I thought to use SESSION vars, dang it I want something as straightforward window.location. - codificador CFH

I'm wondering about the timing. I'm in javascript code here. If I Ajax the session variables, then use window.location="mypage.html" I'm not sure if using the asynchronous form of xmlhttprequest.open() (setting the last param to "true") is going to necessarily guarantee that by the time window.location loads the new page, that asynch Ajax call will have completed, ie. I might arrive at the new page having been loaded into the browser without the session-variable-setting (asynchronous) Ajax call having finished. And I am not going to use a synchronous Ajax call. - codificador CFH

Prueba esto ...

  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.

Respondido 08 Feb 14, 17:02

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();
}

Cómo funciona-

args = {
            "name1" :   "value1",
            "name2" :   "value2",
            "name3" :   "value3",
            "name4" :   "value4"
        };
URL = "a.php";
post_redirect(URL, args);
//Re-directed to page pointed by URL

Respondido 08 Feb 14, 17:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.