Reputation: 11377
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'];
Upvotes: 0
Views: 2107
Reputation: 85518
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')
Upvotes: 3
Reputation: 943134
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.
Upvotes: 0
Reputation: 1295
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
Upvotes: 0