I am pretty new to JavaScript and PHP and hope you can help me with the following - it is needed urgently: :-)

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

Thanks for any help with this ! Tim

share|improve this question
    
And what is the problem..? – 웃웃웃웃웃 Aug 13 '13 at 9:57
    
possible duplicate of this question – KarelG Aug 13 '13 at 9:59
up vote 2 down vote accepted

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') 
share|improve this answer
    
This works perfect - thanks a lot for the fast reply ! Tim – user2571510 Aug 13 '13 at 10:16

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

share|improve this answer
    
Thanks, Shakir ! That helped. – user2571510 Aug 13 '13 at 10:26

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.