martincarlin87
martincarlin87

Reputation: 11042

PHP & JavaScript - Redirecting to a dynamic URL

Just a quick question.

At the moment I have a login script that re-directs to dashboard.php where the user can click a button to retrieve Google Analytics stats for the pre-selected current month and year.

The url for May 2011 is: dashboard.php?month=May&year=2011&submit=Get+Statistics!

So what I am trying to do is have the stats load with the page instead of having to click the button.

The only way I can think to do this is by changing the following line in my login script:

// redirect to secure page
document.location='dashboard.php';

to something like:

// redirect to secure page
document.location='dashboard.php' + <?php echo ?month=currentMonth&year=currentYear&submit=Get+Statistics! ?>';

with the month and year variables set as follows: $currentMonth = date('F'); $currentYear = date('Y');

But this doesn't work, it just goes to dashboard.php as normal.

Any ideas?

Upvotes: 0

Views: 873

Answers (3)

g_thom
g_thom

Reputation: 2810

You're missing the single quote in front of the second string:

document.location='dashboard.php' + '<?php echo "?month=currentMonth&year=currentYear&submit=Get+Statistics!" ?>';

should work in a PHP file.

Upvotes: 2

Alberto Fern&#225;ndez
Alberto Fern&#225;ndez

Reputation: 1598

As long as you are in the same scope, you can get the variables in any part of the code:

// redirect to secure page
document.location='dashboard.php' + <?= "?month=$currentMonth&year=$currentYear&submit=Get+Statistics!"; ?>';

Upvotes: 1

Rodolphe
Rodolphe

Reputation: 497

I don't know if this is when you wrote your post, but your echo is missing quotes :

Try this :

document.location='dashboard.php' + <?php echo "?month=currentMonth&year=currentYear&submit=Get+Statistics!"; ?>';

Upvotes: 1

Related Questions