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'];
2
  • And what is the problem..? Commented Aug 13, 2013 at 9:57
  • possible duplicate of this question Commented Aug 13, 2013 at 9:59

3 Answers 3

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') 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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.