0

I am pretty new to JavaScript and PHP.

I'd like to create a JavaScript function that contains a variable, passes this on to PHP on another page and opens that page.

Here is what I got so far (not working):

My JS:

function test()
{
    $.ajax(   
    {
        url: "my-new-page.php",
        type: "POST",
        data: 
        {
            varJS: "XXX"
        },
            error:function(err)
            {
                alert(err.statusText);
            },
            success: function(data)
            {
                window.open("my-new-page.php");
            }
        });
}

My PHP (on the new page):

$varPHP = $_POST['varJS'];
halfer
  • 19,824
  • 17
  • 99
  • 186
user2571510
  • 11,167
  • 39
  • 92
  • 138

3 Answers3

3

As I understand your question, you just want a simple javascript function that redirects to another page / PHP-script with some params?

my-new-page.php

<?
$varPHP = $_GET['varJS'];
echo $varPHP;
?>

javascript

function reDirect(varJS) {
    var page='my-new-page.php?varJS='+varJS;
    document.location.href=page;
}

reDirect('test') 
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
0

To just open a new window passing it a variable, you can do that within a query string.

Simply call window.open("my-new-page.php?varJS=XXX);

And Handle Query String on my-new-page.php

-Shakir

SHAKIR SHABBIR
  • 1,295
  • 1
  • 14
  • 25
0

The entire point of using Ajax is that it doesn't take the user to a new page. Don't use Ajax.

If you need to make a POST request then generate a form and hidden inputs with document.createElement and friends, append it to the current document, and then call its submit() method.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335